Docker Compose Containers with specific hostpath on nginx

I have two docker containers running with docker-compose and nginx.
I want to run each one on it specific hostpath. (Running them on it own port also will work for me)

http://host/container1
http://host/container2
or 
http://host:80
http://host:81

I got my nginx template from: https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl

  • TensorFlow in nvidia-docker: failed call to cuInit: CUDA_ERROR_UNKNOWN
  • What would be a good docker webdev workflow?
  • What shared libraries are available on Heroku's Docker platform?
  • docker registry v2 : get parent image name
  • how to create docker overlay network between multi hosts?
  • Recursive error shown by docker daemon
  • I’m able to run them separately, So they run fine as single containers with nginx.
    I have tried modifying the location / { proxy_pass .. } to send to different port (localhost:80, localhost:81).

    Added to L237 https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl

    location /container1 {
        proxy_pass {{ trim $proto }}://{{ trim $host }};
    }
    
    location /container2 {
        proxy_pass {{ trim $proto }}://{{ trim $host }}:81;
    }
    

    docker-compose.yml

    version: '2'
    services:
      nginx-container:
        image: nginx
        container_name: nginx-container
        ports:
        # "server_host_port:nginx_container_port"
          - "80:80"
          - "81:81"
        volumes:
          - /etc/nginx/conf.d
          - ./docker-gen/proxy.conf:/etc/nginx/proxy.conf
      docker-gen:
        image: jwilder/docker-gen
        command: -notify-sighup nginx-container -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
        volumes_from:
          - nginx-container
        volumes:
          - /var/run/docker.sock:/tmp/docker.sock:ro
          - ./docker-gen/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl
      container-one:
        build: ./container-one
        image: docker.repository.testing.com/container-one
    # Exposing 8080 for dockergen
        expose:
          - 8080
        environment:
          - spring.profiles.active=docker,testing
          - nginx.host=nginx-container
          - nginx.port=80
          - VIRTUAL_HOST=host
        links:
          - nginx-container
        depends_on:
          - nginx-container
      container-two:
        build: ./container-two
        image: docker.repository.testing.com/container-two
    # Exposing 8080 for dockergen
        expose:
          - 8080
        environment:
          - spring.profiles.active=docker,testing
          - nginx.host=nginx-container
          - nginx.port=81
          - VIRTUAL_HOST=host
        links:
          - nginx-container
          - container-one
        depends_on:
          - nginx-container
          - container-one
    

    As I understand it:
    I have my container-one and container-two running like this:

    container-one -> nginx-container:80
    container-two -> nginx-container:81
    

    Both using the virtual_host “host”

    So I should be able to change “location” in the nginx.tmpl file to point to the host:port on the containers

    However this doesn’t work…
    Sometimes I get the nginx welcome page sometimes I got a 503 or 404

    I’m not sure what I’m missing or doing wrong.
    Is this the right way to configure this?

    References:
    https://github.com/jwilder/nginx-proxy
    https://gist.github.com/soheilhy/8b94347ff8336d971ad0
    https://github.com/nbellocam/sample-site-docker/blob/master/nginx/conf/nginx.conf
    http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

  • Queries about LoadBalancing in Kubernetes cluster
  • how to clean up docker overlay directory?
  • Running gunicorn on ubuntu based image using docker-compose
  • How to get the IP address of the docker host from inside a docker container
  • How to start kubernetes service on NodePort outside service-node-port-range default range?
  • docker-compose error with ghost and an nginx proxy
  • One Solution collect form web for “Docker Compose Containers with specific hostpath on nginx”

    I ended up using two VIRTUAL_HOST, one for each container.

    host.container1.com
    host.container2.com
    

    This worked, although I had to add a new host to match the virtual host.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.