docker容器无法连接到其他网络中的其他容器 [英] docker container can't connect to other container in different network

查看:175
本文介绍了docker容器无法连接到其他网络中的其他容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上运行了2个不同的容器(应用程序和web_server)。它们在同一网络中,因为我将它们放在同一网络中,并且该网络的驱动程序是网桥。

I have 2 different containers running on my server(app and web_server). They are in the same network, because I put them in the same network and that network's driver is bridge.

networks:
  app-network:
    driver: bridge

现在,我有第3个容器,需要连接我提到的2个容器之一。我尝试将第3个容器连接到第1个容器,如下所示:app:9000,但显示未找到主机。我猜这是因为它们位于不同的网络中。我应该怎么做才能使其正常工作?

Now, I have 3rd container, which needs to connect one of the 2 containers I mentioned. I try to connect from 3rd container to 1st container like this: app:9000 , but it says host not found. I guess this is because they are in different networks. What should I do to make it work?

推荐答案

您需要两个容器都加入同一网络。

You need both containers join the same network. either by creating a new network and make then join it or one of them join the other container's network.

docker-compose.yml,您拥有 container1 和 container2

the docker-compose.yml where you have container1 and container2:


  • 定义一个外部属于 container3
  • 的网络
  • 将这两个容器之一添加到外部网络中,如第一个片段所示

您有 container3 的docker-compose.yml将是一个普通的docker-compose

the docker-compose.yml where you have container3 will be a normal docker-compose with a bridge network.


您需要确保首先启动第三个容器,以便首先创建第三个网络,或者使用docker cli手动创建第三个网络,并将定义更改为外部

you need to make sure to start the third container first in order to allow the third network to be created first or you can create the third network manually using docker cli and change the definition to external



#docker-compose.yml

container1:
  networks:
    - first_network
    - second_network

networks:
  first_network:
    driver: bridge
  second_network:
    external: true



#docker-compose.yml
container3:
  networks:
    - second_network

networks:
  second_network:
    driver: bridge

这篇关于docker容器无法连接到其他网络中的其他容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆