How to join the default bridge network with docker-compose v2?
I tried to setup an nginx-proxy container to access my other containers via subdomains on port 80 instead of special ports. As you can guess, I could not get it to work.
I'm kind of new to docker itself and found that it's more comfortable for me to write docker-compose.yml
files so I don't have to constantly write long docker run ...
commands. I thought there's no difference in how you start the containers, either with docker
or docker-compose
. However, one difference I noticed is that starting the container with docker
does not create any new networks, but with docker-compose
there will be a xxx_default
network afterwards.
I read that containers on different networks cannot access each other and maybe that might be the reason why the nginx-proxy is not forwarding the requests to the other containers. However, I was unable to find a way to configure my docker-compose.yml
file to not create any new networks, but instead join the default bridge network like docker run
does.
I tried the following, but it resulted in an error saying that I cannot join system networks like this:
networks:
default:
external:
name: bridge
I also tried network_mode: bridge
, but that didn't seem to make any difference.
How do I have to write the docker-compose.yml
file to not create a new network, or is that not possible at all?
Bonus question: Are there any other differences between docker
and docker-compose
that I should know of?
Solution 1:
Adding network_mode: bridge
to each service in your docker-compose.yml
will stop compose from creating a network.
If any service is not configured with this bridge (or host), a network will be created.
Tested and confirmed with:
version: "2.1"
services:
app:
image: ubuntu:latest
network_mode: bridge