简体   繁体   English

一个 Docker Container 如何找到另一个 Docker Container 的 IP 地址?

[英]How can one Docker Container find the IP address of another Docker Container?

Working on my first MSA application, and trying to figure out how a Docker container can find the IP address of another Docker Container?处理我的第一个 MSA 应用程序,并试图弄清楚 Docker 容器如何找到另一个 Docker 容器的 IP 地址? My containers know the name of the other container it needs to connect to, but that is it.我的容器知道它需要连接的另一个容器的名称,但就是这样。 I can't use a "--link" option or docker-compose as the components can come an go at different times, and I need a programmatic way to find the IP address of the other containers and start connections to them.我不能使用“--link”选项或 docker-compose,因为组件可以在不同时间出现 go,我需要一种编程方式来找到 IP 到其他容器的连接地址

I have to think this problem has already been solved long long ago, but my GoogleFoo must be very weak today, as I can't find anything!我不得不认为这个问题早就解决了,但是我的GoogleFoo今天一定很弱,因为我找不到任何东西!

Thanks for your help: :)谢谢你的帮助: :)

AAR: So the issue is that on the default 'bridge' bridge network, the Docker DNS does NOT keep track of container names...all other network types it will. AAR:所以问题在于,在默认的“bridge”桥接网络上,Docker DNS 不会跟踪容器名称……它会跟踪所有其他网络类型。

So the accepted solution below is to use any other network and have the containers use that network.所以下面接受的解决方案是使用任何其他网络并让容器使用该网络。

I created another bridge network so that I can still do testing/dev work, and my containers can find one another by name now.我创建了另一个桥接网络,以便我仍然可以进行测试/开发工作,并且我的容器现在可以通过名称找到另一个。 For production, I will have to change the network to something like a 'macvlan' type.对于生产,我必须将网络更改为“macvlan”类型。

If you are using something like Kubernetes, then you will need a Service Discovery solution like Zookeeper or Consul.如果您使用的是 Kubernetes 之类的东西,那么您将需要像 Zookeeper 或 Consul 这样的服务发现解决方案。

You have to define network in your docker-compose as below您必须在 docker-compose 中定义网络,如下所示

networks:
  my-network:

And then make both the containers part of the same network.然后使两个容器成为同一网络的一部分。

networks:
  - my-network

And they can access other docker service port using docker service name.他们可以使用 docker 服务名称访问其他 docker 服务端口。

EDIT 2: Without dokcer-compose I am able to communicate 2 containers as below:编辑 2:没有 dokcer-compose,我可以通信 2 个容器,如下所示:

1) Created netwrok - my-netwrok 1) 创建网络 - my-netwrok

docker network create -d bridge my-network

2) Started 2 service unders same network 2)在同一网络下启动2个服务

docker run -d --name eureka --network=my-network eureka-service:1.0
docker run -d --name facility --network=my-network facility-service:1.0

3) Logged in to facility container 3) 登录到设施容器

docker exec -it facility bash

4) pinged eureka from inside facility container 4)从设施容器内ping eureka

bash-4.4# ping eureka
PING eureka (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.149 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.078 ms

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

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