简体   繁体   English

如何使用docker-compose v2加入默认网桥?

[英]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. 我尝试设置一个nginx-proxy容器,通过端口80上的子域而不是特殊端口访问我的其他容器。 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. 我对docker本身有点新意,发现编写docker-compose.yml文件对我来说更舒服,所以我不必经常写长docker run ...命令。 I thought there's no difference in how you start the containers, either with docker or docker-compose . 我认为使用dockerdocker-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. 但是,我注意到的一个区别是,使用docker启动容器不会创建任何新网络,但是使用xxx_default docker-compose会有一个xxx_default网络。

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. 我读到不同网络上的容器无法互相访​​问,这可能是nginx-proxy没有将请求转发给其他容器的原因。 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. 但是,我无法找到一种方法来配置我docker-compose.yml文件以不创建任何新网络,而是加入默认的桥接网络,如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. 我也尝试过network_mode: bridge ,但这似乎没有任何区别。

How do I have to write the docker-compose.yml file to not create a new network, or is that not possible at all? 如何编写docker-compose.yml文件以不创建新网络,或者根本不可能?

Bonus question: Are there any other differences between docker and docker-compose that I should know of? 奖金问题:我应该知道dockerdocker-compose之间是否还有其他差异?

Adding network_mode: bridge to each service in your docker-compose.yml will stop compose from creating a network. network_mode: bridge添加到docker-compose.yml每个服务将停止创建网络。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM