Multi-Domain Docker Containers

Use case

We have several server applications in the same development environment, each application is bundled in a Docker container, e.g: "Container A" and "Container B".

With Docker those applications have the same IP address. One way to differentiate and access to an specific application is exposing different ports.

/galleries/docker-multidomain/ip.thumbnail.png

Containers exposing the same IP address and different ports

But that solution is a little bit confusing, does 8080 mean we are accessing to "application A"?

It would be simpler and easier to remind something like:

/galleries/docker-multidomain/domain.thumbnail.png

Accessing applications by domain name

Get that extra semantic value is much simpler than I thought at the beginning and you will see below.

How to Configure Multi-Domain Reverse Proxy

I said it is easy, because we almost have to do nothing, another container will do it for us, especifically we are going to use nginx-proxy, it will automatically generate the required NGINX configurations.

So, we will have 2 applications + 1 proxy, that is 3 containers.

Note

You can download the full example at https://github.com/carlosvin/docker-reverse-proxy-multi-domain

/galleries/docker-multidomain/proxy.thumbnail.png

3 containers, 2 applications + 1 proxy

Example Project Structure

  • docker-compose.yaml (Main configuration file describing architecture in previous picture)
  • a (Application A directory)
    • Dockerfile (Container A configuration file)
  • b (Application B directory)
    • Dockerfile (Container B configuration file)

Ver proyecto.

Architecture Configuration (docker-compose)

The relationships between containers is the most interesting part in this example.

docker-reverse-proxy-multi-domain/docker-compose.yaml (Source)

a:
  build: a
  environment:
    VIRTUAL_HOST: a.domain.com
  restart: always

b:
  build: b
  environment:
    VIRTUAL_HOST:  b.domain.com
  restart: always

nginx-proxy:
  image: jwilder/nginx-proxy
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro

  restart: always
  privileged: true
  • Lines 4 and 10: we configure the domain name for each application.
  • From line 13 there is proxy configuration (copy/paste part).
  • In lines 2 and 8 we tell docker-compose has to build Docker images within specified directory. For example, in line 2, we are saying that docker-compose has to build a Docker image using ./a/Dockerfile file.

Application Image Configuration

docker-reverse-proxy-multi-domain/a/Dockerfile (Source)

FROM httpd:2.4
RUN echo "<html><body><h1>A</h1>App A works!</body></html>" > /usr/local/apache2/htdocs/index.html

Line 1: We import an image with an apache server.

Line 2: It serves a file that prints "Host A" as default page.

The configuration for application B is pretty much the same:

docker-reverse-proxy-multi-domain/b/Dockerfile (Source)

FROM httpd:2.4
RUN echo "<html><body><h1>B</h1>App B works!</body></html>" > /usr/local/apache2/htdocs/index.html

Adding domain names to your development environment configuration

In Linux we just have to map the local address to domain names you have chosen, in the example a.domain.com and b.domain.com.

1
2
3
4
5
#/etc/hosts
127.0.0.1             localhost.localdomain localhost
::1                 localhost6.localdomain6 localhost6
127.0.0.1   a.domain.com
127.0.0.1   b.domain.com

I just added 4 and 5 lines.

Everything ready!

Now we just have to test the example:

docker-compose build
docker-compose up

The 3 containers are running now.

So we can open our favourite web browser and go to a.domain.com. It will show App A works!. If we go to b.domain.com then we will see App B works!.

/galleries/docker-multidomain/a.screenshot.thumbnail.png

a.domain.com

/galleries/docker-multidomain/b.screenshot.thumbnail.png

b.domain.com

Note

In most of Linux distros you will need privileges to run Docker commands (sudo).

Comments

Comments powered by Disqus