67

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?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
noone
  • 19,520
  • 5
  • 61
  • 76
  • [v1 format](https://docs.docker.com/compose/compose-file/compose-file-v1/) compose files won't create a new network, but require `links` to be setup to talk to other containers on the default bridge. – Matt May 03 '17 at 08:33
  • v1 is due to be deprecated, its use should be avoided. – Rawkode May 03 '17 at 08:44
  • Yes, I was using version 2 format. Thanks for the hint. – noone May 03 '17 at 08:45
  • I just used that syntax for version 3 where it works just fine. i did not use the default bridge but another one created by another docker-compose.yml containing the nginx. the remaining problem is the hostnames with the underscore in iit – U.V. Oct 26 '21 at 17:10

1 Answers1

127

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
Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • 3
    This breaks discovery. So if you have >1 containers that need to talk to each other it doesn't work. – John Eikenberry Jan 08 '18 at 23:49
  • 3
    @JohnEikenberry While it can make discovery tricky, it does not break it. The user can still use `links` syntax within compose. – Rawkode Apr 06 '18 at 12:35
  • Docker stacks don't support `network_mode`, is there a similar solution to making this work with them? – Micah Zoltu Sep 02 '18 at 07:31
  • Docker Swarm has lost. Kubernetes has won. My advice? use `docker-compose.yml` was development only, stick to `version: 2.4` and forget `3` exists :+1: – Rawkode Sep 03 '18 at 08:14
  • In e a stack deploy you label the network as external in the networks stanza to achieve the same functionality as this in `docker-compose` – Brady Feb 12 '20 at 02:50
  • Beware that joining the default bridge will break DNS resolution of container names. So e.g. `ping app` will stop working. – rustyx Oct 30 '22 at 19:25