如何使用docker-compose v2加入默认网桥网络? [英] How to join the default bridge network with docker-compose v2?
问题描述
我尝试设置 nginx-proxy 容器,以通过端口上的子域访问我的其他容器80个而不是特殊的端口。如您所料,我无法正常工作。
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.
我对docker本身还是很陌生的,发现我写<$ c更舒服$ c> docker-compose.yml 文件,这样我就不必不断编写 docker run ...
命令。我认为使用 docker
或 docker-compose
启动容器的方式没有什么不同。但是,我注意到的一个区别是,使用 docker
启动容器不会创建任何新的网络,而是使用 docker-compose
之后将有一个 xxx_default
网络。
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.
我读到不同网络上的容器无法相互访问,也许这可能就是nginx-proxy无法将请求转发到其他容器的原因。但是,我无法找到一种方法来配置我的 docker-compose.yml
文件以不创建任何新网络,而是加入默认的网桥网络,例如 docker run
确实。
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
我也尝试过 network_mode:bridge
,但这似乎并没有有所不同。
I also tried network_mode: bridge
, but that didn't seem to make any difference.
如何编写 docker-compose.yml
文件而不创建新文件网络,还是根本不可能?
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
和 docker-compose
我应该知道吗?
Bonus question: Are there any other differences between docker
and docker-compose
that I should know of?
推荐答案
添加 network_mode:桥接
到您的 docker-compose.yml
中的每个服务将停止从创建网络。
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.
经过测试并确认:
version: "2.1"
services:
app:
image: ubuntu:latest
network_mode: bridge
这篇关于如何使用docker-compose v2加入默认网桥网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!