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?