Nginx inside docker container can't talk to other containers (running jenkins)

I'm trying to set up a nginx proxy inside docker that will listen for web traffic and then based on virtualhosts forward traffic to differerat jenkins instances also running inside docker containers. To orchestrate this I use docker-compose.

I have managed to:

  • Start up jenkins instances and talk to them directly
  • Start nginx and configure it to act as a proxy for a webserver running on another host

But when I try to let the nginx server route the traffic inside docker I cant get it to work, just getting 502-Bag Gateway errors and the folowing in the nginx logs:

[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost:8080"

Relevant parts of my docker-compose.yml:

httpd:
  build: httpd
  ports:
      - "8080:8080"
  links:
      - "server_jenkins"
      - "clients_jenkins"
  restart: always

server_jenkins:
  image: jenkins
  ports:
      - "8081:8080"
  restart: always

clients_jenkins:
  image: jenkins
  ports:
      - "8082:8080"
  restart: always

The httpd/Dockerfile looks like this:

FROM nginx
RUN rm -rf /etc/nginx
COPY config /etc/nginx/

And relevant parts of nginx.conf looks like this (only proxying one jenkins instance):

http {
    server {
        listen       8080;
        server_name server.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://127.0.0.1:8081;
        }
    }
    server {
        listen       8080;
        server_name clients.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://127.0.0.1:8082;
        }
    }
}

I have tried the following variants of the first proxy_pass url (and some other that I don't remember):

When running with --x-networking I changed the link statements to container_name in docker-compose.yml

Versions:

  • docker: 1.9.1, build a34a1d5
  • docker-compose: 1.5.2, build 7240ff3
  • OS: Debian 8 (running inside Virtualbox)

I am deleting my previous answer as I was answering something completely different. I found your problem, its the http://127.0.0.1 and ports which is wrong, 127.0.0.1 inside nginx's conf means the httpd container itself in this case not the host.

If you re-define that to server_jenkins and clients_jenkins and also suffix the 8080 port for both of those, then it should work.

server {
    listen       8080;
    server_name server.build.example.com;
    root /tmp;

    location / {
        proxy_pass   http://server_jenkins:8080;
    }
}
server {
    listen       8080;
    server_name clients.build.example.com;
    root /tmp;

    location / {
        proxy_pass   http://clients_jenkins:8080;
    }
}

Also, I noticed that the http directive causes errors, but as you mentioned the file you have provided can be just a snippet.

Found the problem. I was confused over how portmapping works.

The working config: docker-compose.yml:

httpd:
  build: httpd
  ports:
      - "8080:8080"
  links:
      - "server_jenkins"
      - "clients_jenkins"
  restart: always

server_jenkins:
  image: jenkins
  restart: always

clients_jenkins:
  image: jenkins
  restart: always

nginx.conf:

http {
    server {
        listen       8080;
        server_name server.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://server_jenkins:8080;
        }
    }
    server {
        listen       8080;
        server_name clients.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://client_jenkins:8080;
        }
    }
}

Ie. There are no need for mapping the ports.

Please provide clear error messages. “[…] I cant get it to work.[…]” isn’t sufficient at all.

Well I get 502-Bad Gateway from the nginx server when I go to localhost:8080