如何使用 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 本身有点陌生,发现编写 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?
推荐答案
向 docker-compose.yml
中的每个服务添加 network_mode: bridge
将阻止 compose 创建一个网络.
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屋!