简体   繁体   English

用于通信2个Docker容器的IP地址是什么?

[英]What is the ip address used to communicate 2 docker containers?

I have 2 docker containers and they are connected using docker network. 我有2个Docker容器,它们使用docker网络连接。 I run the following command twice. 我两次运行以下命令。

docker network connect [network id] [container id]

The first container need to communicate with the second container by HTTP. 第一个容器需要通过HTTP与第二个容器进行通信。

I set the ip to 172.17.0.1, but look like the ip is wrong. 我将IP设置为172.17.0.1,但看起来IP错误。

I believe this is a common question for those experience docker users. 我相信这是那些经验丰富的Docker用户的常见问题。

Can anyone help me? 谁能帮我?

Regards, Jimmy 问候,吉米

It might be the case that the container didn't connect to the network. 可能是容器未连接到网络的情况。 For checking that you can inspect the network to which the containers are connected so that they can communicate. 为了进行检查,您可以检查容器连接到的网络,以便它们可以通信。 Use the command docker network inspect network_name 使用命令docker network inspect network_name

If the network is default network bridge you can see the following o/p before the container are attached to it: 如果网络是默认的网桥,则在将容器连接到它之前,您可以看到以下o / p:

[
   {
       "Name": "bridge",
       "Id": "f7ab26d71dbd6f557852c7156ae0574bbf62c42f539b50c8ebde0f728a253b6f",
       "Scope": "local",
       "Driver": "bridge",
       "IPAM": {
           "Driver": "default",
           "Config": [
               {
                   "Subnet": "172.17.0.1/16",
                   "Gateway": "172.17.0.1"
               }
           ]
       },
       "Containers": {},
       "Options": {
           "com.docker.network.bridge.default_bridge": "true",
           "com.docker.network.bridge.enable_icc": "true",
           "com.docker.network.bridge.enable_ip_masquerade": "true",
           "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
           "com.docker.network.bridge.name": "docker0",
           "com.docker.network.driver.mtu": "9001"
       },
       "Labels": {}
   }
]

And once two containers are attached then the o/p will be as shown below: 一旦连接了两个容器,则o / p将如下所示:

{[
    {
        "Name": "bridge",
        "Id": "f7ab26d71dbd6f557852c7156ae0574bbf62c42f539b50c8ebde0f728a253b6f",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Config": [
                {
                    "Subnet": "172.17.0.1/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Containers": {
            "3386a527aa08b37ea9232cbcace2d2458d49f44bb05a6b775fba7ddd40d8f92c": {
                "EndpointID": "647c12443e91faf0fd508b6edfe59c30b642abb60dfab890b4bdccee38750bc1",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "94447ca479852d29aeddca75c28f7104df3c3196d7b6d83061879e339946805c": {
                "EndpointID": "b047d090f446ac49747d3c37d63e4307be745876db7f0ceef7b311cbba615f48",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "9001"
        },
        "Labels": {}
    }
]

As you can see there is the ip-addresses of the two containers in the o/p. 如您所见,o / p中有两个容器的ip地址。

Now you can attach to a container and try to ping the other container to see that they can communicate. 现在,您可以连接到容器,并尝试ping其他容器以查看它们是否可以通信。

You can follow docker doc for complete details 您可以关注Docker文档以获取完整详细信息

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

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