3

I'm trying to setup an nginx proxy for my docker containers to use simple subdomains instead of ports. I like using docker-compose to startup my containers and since I'm quite new to docker in general, I started using v2 format right away.

I've spend quite some time figuring out why this very popular and seemingly simple to use nginx proxy container did not work for me. It turned out that it is somehow related to my use of docker-compose v2.

I will post the docker-compose.yml files I was using first, which did not work for some reason:

version: '2'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy:alpine
    container_name: nginx-proxy
    network_mode: bridge
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    environment:
      - ENABLE_IPV6=true
      - DEFAULT_HOST=domain.com

  whoami:
    image: jwilder/whoami
    network_mode: bridge
    environment:
      - VIRTUAL_HOST=whoami.local

And this is the example Jenkins container to test it:

version: "2"

services:
  jenkins:
    image: jenkins:2.46.2-alpine
    restart: always
    hostname: jenkins.domain.com
    network_mode: bridge
    expose:
      - 8080
      - 50000
    ports:
      - 8080:8080
      - 50000:50000
    volumes:
      - /srv/jenkins:/var/jenkins_home
    environment:
      - VIRTUAL_HOST=jenkins.domain.com
      - VIRTUAL_PORT=8080

Now I will post the configuration which actually did work out of the box for me:

nginx-proxy:
  image: jwilder/nginx-proxy:alpine
  container_name: nginx-proxy
  ports:
    - 80:80
    - 443:443
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro
  environment:
    - ENABLE_IPV6=true
    - DEFAULT_HOST=domain.com

whoami:
  image: jwilder/whoami
  environment:
    - VIRTUAL_HOST=whoami.local

And for the Jenkins container:

jenkins:
  image: jenkins:2.46.2-alpine
  restart: always
  hostname: jenkins.domain.com
  expose:
    - 8080
    - 50000
  ports:
    - 8080:8080
    - 50000:50000
  volumes:
    - /srv/jenkins:/var/jenkins_home
  environment:
    - VIRTUAL_HOST=jenkins.domain.com
    - VIRTUAL_PORT=8080

The only real difference I see is the removal of network_mode: bridge. I've added that when I noticed that with v2 there are seperate networks being created, but with v1 (or a simple docker run), they end up on the same network. Using network_mode: bridge seemed to solve this issue.

Besides this, it's only a structural change in the docker-compose.yml files, but there must be some other differences which stop this setup from working.

Since V1 is deprecated and I would like to use v2 format... what do I have to change to make docker-compose v2 act like v1 and get the proxy to work correctly?

2
  • What error are you seeing? Commented May 8, 2017 at 10:53
  • There is actually no error. The proxy is just not working and does not forward the requests to the other containers. Commented May 8, 2017 at 12:50

1 Answer 1

2

You need to make sure the containers are on the same network. In nginx-proxy, it won't add an upstream setting if it can't reach the node. You should see something like:

$ docker exec -it nginx-proxy cat /etc/nginx/conf.d/default.conf
# ....
# whoami.local
upstream whoami.local {
                                ## Can be connect with "nginxproxy_default" network
                        # nginxproxy_whoami_1
                        server 172.19.0.3:8000;
}
# ....

If that upstream section is empty, without the comments and server line, then it's unable to find a common docker network to reach the container and it won't be able to route traffic.

I'm seeing that with the following compose file:

version: '2'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy:alpine
    container_name: nginx-proxy
    ports:
      - 8080:80
      - 8443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    environment:
      - ENABLE_IPV6=true
      - DEFAULT_HOST=domain.com

  whoami:
    image: jwilder/whoami
    environment:
      - VIRTUAL_HOST=whoami.local

And I can verify it with:

$ curl -H "Host: whoami.local" http://localhost:8080
I'm b066afdb6e45

With Jenkins, when you spin that up with a separate compose file (actually a separate compose project which happens by default when your in a different directory name), it will get a separate default network. The easiest solution I have is to use an external network. First you create it directly in docker:

$ docker network create proxynet

And then your compose file would include that external network:

version: '2'

networks:
  proxynet:
    external: true

services:
  nginx-proxy:
    image: jwilder/nginx-proxy:alpine
    container_name: nginx-proxy
    ports:
      - 8080:80
      - 8443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - proxynet
    environment:
      - ENABLE_IPV6=true
      - DEFAULT_HOST=domain.com

  whoami:
    image: jwilder/whoami
    environment:
      - VIRTUAL_HOST=whoami.local
    networks:
      - proxynet

You would do the same with Jenkins:

version: "2"

networks:
  proxynet:
    external: true

services:
  jenkins:
    image: jenkins:2.46.2-alpine
    restart: always
    hostname: jenkins.domain.com
    networks:
      - proxynet
    expose:
      - 8080
      - 50000
    ports:
      - 8080:8080
      - 50000:50000
    volumes:
      - /srv/jenkins:/var/jenkins_home
    environment:
      - VIRTUAL_HOST=jenkins.domain.com
      - VIRTUAL_PORT=8080
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. I will test it later and verify that it works. What I do not understand though: Where is the difference between setting up a shared external network for both containers, or using network_mode: bridge in both to put them on the same default bridge network?
I'd need to test it, but bridge is the default network mode, so it likely continues to do the default thing where it makes a unique network per project (a v2 feature).
Listing the networks does not list any additional networks (besides the 3 default ones). However without network_mode: bridge, there are 2 additional networks (one for nginx and one for jenkins).
Indeed it does. I can't get network_mode: bridge to break in my environment, as long as everything is there nginx_proxy sees each of the containers and relays the traffic.
Hhmmm this is weird. I thought docker containers work the same on every docker platform... Anyway, explicitly using a shared external network works, so I'm going with this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.