How to join the default bridge network with docker-compose v2?

前提是你 提交于 2019-11-30 14:29:46

问题


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?


回答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


来源:https://stackoverflow.com/questions/43754095/how-to-join-the-default-bridge-network-with-docker-compose-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!