Subdomains, Nginx-proxy and Docker-compose

If you are already using docker-compose I recommend using the jwilder nginx-proxy container.

https://github.com/jwilder/nginx-proxy

This allows you to add unlimited number of web service containers to the backend of the defined nginx proxy, for example:

nginx-proxy:
  image: jwilder/nginx-proxy
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - "/etc/nginx/vhost.d"
    - "/usr/share/nginx/html"
    - "/var/run/docker.sock:/tmp/docker.sock:ro"
    - "nginx_certs:/etc/nginx/certs:rw"
nginx:
  build:
   context: ./docker/nginx/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host1.com
nginx_2:
  build:
   context: ./docker/nginx_2/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host2.com
apache_1:
  build:
   context: ./docker/apache_1/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host3.com

The nginx-proxy mount the host docker sock file in order to get information about the other containers running, if any of them have the env variable VIRTUAL_HOST set then it will add it to its configuration.


Unfortunately nginx doesn't support sub-domains on IP addresses like that.

You would either have to modify the clients hosts file (which you said you didn't want to do)...


Or you can just set your nginx to redirect like so:

location /jenkins {
    proxy_pass http://jenkins:8080;
    ...
}

location /other-container {
    proxy_pass http://other-container:8080;
}

which would allow you to access jenkins at 192.168.1.2/jenkins


Or you can try and serve your different containers through different ports. E.g:

server {
    listen 8081;
    location / {
        proxy_pass http://jenkins:8080;
        ...
    }
}

server {
    listen 8082;
    location / {
        proxy_pass http://other-container:8080;
        ...
    }
}

And then access jenkins from 192.168.1.2:8081/