nginx-proxy: Unable to profixy a container started with docker-compose version 2

Hi,

I wanted to implement the nginx-proxy to serve several nginx servers so I followed the example on the homepage and it works fine. However, thing start being more complex when I try to implement it with my real servers. In short the nginx-proxy is up but web servers are not proxified and I can only see a 503 status code from the proxy.

Nginx-proxy is catching events and is generating the Nginx default configuration file every time I start or stop a container.

dockergen.1 | 2016/07/08 13:38:53 Generated '/etc/nginx/conf.d/default.conf' from 1 containers
dockergen.1 | 2016/07/08 13:38:53 Running 'nginx -s reload'
dockergen.1 | 2016/07/08 13:38:53 Watching docker events
dockergen.1 | 2016/07/08 13:38:53 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'

But when I start on of my container I can see the following error message.

dockergen.1 | 2016/07/08 13:39:27 Received event start for container 69b904a872f0
dockergen.1 | 2016/07/08 13:39:28 Received event start for container 57e7ee6e4496
dockergen.1 | 2016/07/08 13:39:28 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
dockergen.1 | 2016/07/08 13:39:28 Running 'nginx -s reload'
dockergen.1 | 2016/07/08 13:39:28 Error running notify command: nginx -s reload, exit status 1
dockergen.1 | 2016/07/08 13:39:28 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'

I had a look at the Nginx-proxy Default configuration and found that the generated file had no information in it about the web server I wanted to proxify.

upstream baz.bar.local {
}
server {
    server_name baz.bar.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://baz.bar.local;
    }
}

Has you can see there is nothing in the upstream section

upstream baz.bar.local {
}

I have done a lot of tests to understand what going on and finally found that problem only happens when I’m starting a container using docker-compose in version 2 because in other cases it works fine.

Here is a chunk of the Default Nginx file auto-generated

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
    access_log /var/log/nginx/access.log vhost;
    return 503;
}
upstream bar.bar.local {
}
server {
    server_name bar.bar.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://bar.bar.local;
    }
}
upstream foo.bar.local {
                ## Can be connect with "bridge" network
            # whoamiversion1_whoami3_1
            server 172.17.0.4:8000;
}
server {
    server_name foo.bar.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://foo.bar.local;
    }
}
upstream red.bar.local {
                ## Can be connect with "bridge" network
            # whoami4
            server 172.17.0.3:8000;
}
server {
    server_name red.bar.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://red.bar.local;
    }
}

Here is the workflow I have done to reproduce it

  1. Start the nginx-proxy container
$ docker run -d --name nginx-proxy -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy
  1. Start a container that needs to be proxified using docker command
$ docker run -e VIRTUAL_HOST=red.bar.local --name whoami4 jwilder/whoami
  1. Start a container using docker-compose command version 2

The whoami_version-1/docker-compose.yml file

version: '2'
services:
  whoami1:
    image: jwilder/whoami
    environment:
      - VIRTUAL_HOST=bar.bar.local

$ docker-compose up
  1. Start a container using docker-compose command version 1
whoami3:
  image: jwilder/whoami
  environment:
    - VIRTUAL_HOST=foo.bar.local

$ docker-compose up

I have been able to reproduce it several time on my local machine. I’m using the mac beta of Docker

$ docker --version
Docker version 1.12.0-rc3, build 91e29e8, experimental
$ docker-compose --version
docker-compose version 1.8.0-rc1, build 9bf6bc6

I think this issue a version a bit weird but I have not been able to find a better root cause.

Does anyone is able to reproduce ?

I have wrapped everything in a shell to help test easily. Unzip the archive and run startServers.sh

nginx_proxy_test.zip

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 18 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same issues, and solved it by creating a network called nginx-proxy:

docker create network nginx-proxy

Then I added the following to the bottom of my docker-compose file:

networks:
  default:
    external:
      name: nginx-proxy

Here is my complete docker-compose.yml:

version: '2'

services:

  prod:
    image: ${COMPOSE_PROJECT_NAME}-prod
    container_name: ${COMPOSE_PROJECT_NAME}-prod
    environment:
      - VIRTUAL_HOST=${COMPOSE_PROJECT_NAME}
    volumes:
      - ./prod/${COMPOSE_PROJECT_NAME}/web:/var/www/html/
      - ./prod/${COMPOSE_PROJECT_NAME}/data:/var/lib/mysql
    restart: always


  dev:
    image: ${COMPOSE_PROJECT_NAME}-dev
    container_name: ${COMPOSE_PROJECT_NAME}-dev
    environment:
      - VIRTUAL_HOST=${COMPOSE_PROJECT_NAME}.dev
    volumes:
      - ./dev/${COMPOSE_PROJECT_NAME}/web:/var/www/html
      - ./dev/${COMPOSE_PROJECT_NAME}/data:/var/lib/mysql


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


networks:
  default:
    external:
      name: nginx-proxy

@briansrepo Thanks a lot, it works for me, but the command of creating a new network should be docker network create proxy-nginx

FYI: A way to get your v2 config file to work is to add:

network_mode: “bridge” to your docker-compose.yml file.

i.e. :

version: '2'
services:
  webserver:
    image: php:5.6-apache
    environment:
     - VIRTUAL_HOST=your.hostname.com
    network_mode: "bridge"

I had the same problem, and I added to my docker-compose file this line :

network_mode: bridge

Whatever is the network_mode name, if you have created a new network. In my case, I use the default network.

I added this line in my gitlab docker-compose, it was the one who make crash the reverse proxy.

Hi, have look at this comment https://github.com/jwilder/nginx-proxy/issues/478#issuecomment-230502324. What is probably happening is that docker-gen knows about the container but there is no common network. This is a known bug… generated config should probably just skip it totally or generate something valid in upstream {} indicating that there is no way of reaching the container(s).

@flocalhost should VIRTUAL_PORT in the second config really be 8070 not 8069? that is assuming the odoo:10 image will losten on 8069

youre absolutely right. thanks a lot. worked hours on that problem

@flocalhost should VIRTUAL_PORT in the second config really be 8070 not 8069? that is assuming the odoo:10 image will losten on 8069