简体   繁体   中英

Why docker containers are not pingable to each other through container name in the default network name as bridge and driver bridge

I have launched two docker containers named as c1 and c2 in the default docker network named as bridge with bridge driver through the below command.

docker container run -dit --name c1 centos:7
docker container run -dit --name c2 centos:7

So both containers are in the same network

ubuntu@master-node:~$ docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "7301725ee3bdfd470c67c202532e",
        "Created": "2021-11-30T13:39:25.074992675Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "40378c3bf6219ad34b3da9debdf7ef1": {
                "Name": "c1",
                "EndpointID": "f170b34a405483ebb90737a2df255",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "18cbe312f339001c159d30b1a53ec80": {
                "Name": "c2",
                "EndpointID": "fbeb085a049f4127f9411e0b86ca7",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },

and both should be pingable to each other through container-name and IP Address. But Both are pingable through IP Address.

[root@309b8307b0bc /]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.092 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.077 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.070 ms

and they are not pingable with the container name and showing the below message.

[root@309b8307b0bc /]# ping c1
ping: c1: Name or service not known

Now I have created one more network myself named as mynet with bridge driver.

docker network create --attachable --driver bridge mynet

and launched two more docker containers named as c3 and c4 inside mynet .

ubuntu@master-node:~$ docker container run -dit --name c3 --network mynet myimage
9456b0be0875fe89f5e6ff2135598ec396
ubuntu@master-node:~$ docker container run -dit --name c4 --network mynet myimage
29cb726f195a3d7ae734b8a960eaf56fc9

But now I tried with this network with same network driver and then both containers are pingable to each other through IP Address and container name as well.

[root@3c7774229cb7 /]# ping 172.20.0.2
PING 172.20.0.2 (172.20.0.2) 56(84) bytes of data.
64 bytes from 172.20.0.2: icmp_seq=1 ttl=64 time=0.108 ms
64 bytes from 172.20.0.2: icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from 172.20.0.2: icmp_seq=3 ttl=64 time=0.085 ms
^C
--- 172.20.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2047ms
rtt min/avg/max/mdev = 0.085/0.092/0.108/0.015 ms
[root@3c7774229cb7 /]# ping c3
PING c3 (172.20.0.2) 56(84) bytes of data.
64 bytes from c3.mynet (172.20.0.2): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from c3.mynet (172.20.0.2): icmp_seq=2 ttl=64 time=0.075 ms
64 bytes from c3.mynet (172.20.0.2): icmp_seq=3 ttl=64 time=0.082 ms
^C
--- c3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2038ms
rtt min/avg/max/mdev = 0.052/0.069/0.082/0.016 ms

Now I am not able to understand c1 and c2 are not pingable to each other through container name with default network and driver bridge and c3 and c4 are pingable to each other in inside named as mynet while I created this network with same driver bridge.

This is by design. For docker's embedded DNS you need a user created network:

By default, a container inherits the DNS settings of the host, as defined in the /etc/resolv.conf configuration file. Containers that use the default bridge network get a copy of this file, whereas containers that use a custom network use Docker's embedded DNS server, which forwards external DNS lookups to the DNS servers configured on the host.

Ref: https://docs.docker.com/config/containers/container-networking/#dns-services

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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