사용자 생성 bridge network 이용 시, container 이름을 통한 DNS resolution이 되지 않는 현상

Created 2022-03-11

Problem

사용자 정의 bridge network는 해당 네트워크에 속한 container 사이에 DNS resolution을 제공하기 때문에 IP가 아닌 container 이름을 통해 접근할 수 있다. 이와 관련해 실제 container 이름을 통해 DNS reolustion이 되는지 테스트를 진해앴으며 예외 케이스가 발생했다.

bridge network에 대한 설명은 아래 Docker documentation을 참고한다.

정상적인 테스트 케이스는 아래와 같다.

  • 사용자 정의 bridge network 생성 및 조회

# test 이름을 갖는 bridge network 생성
$ docker network create -d bridge test
a23414a0e7620943a4335fde5ed15c1bd9cfd41be4f81ab44b8d27f0ff268185

# test network 조회
$ docker network inspect test
[
    {
        "Name": "test",
        "Id": "a23414a0e7620943a4335fde5ed15c1bd9cfd41be4f81ab44b8d27f0ff268185",
        "Created": "2022-03-11T10:25:21.362459215+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.96.0/20",
                    "Gateway": "192.168.96.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
  • 테스트 container_1, container_2 container 생성

# container_1 생성
$ docker run --rm -tid --network test --name container_1 alpine
421baebd4d86e60ba66bc4d710746012cac01c8597cc3bcbcf299c3510e2accb
# container_2 생성
$ docker run --rm -tid --network test --name container_2 alpine
  • test bridge network에 위에서 생성한 2개의 container가 연결되어있는지 확인

$ docker network inspect test
[
    {
        "Name": "test",
        "Id": "a23414a0e7620943a4335fde5ed15c1bd9cfd41be4f81ab44b8d27f0ff268185",
        "Created": "2022-03-11T10:25:21.362459215+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.96.0/20",
                    "Gateway": "192.168.96.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "421baebd4d86e60ba66bc4d710746012cac01c8597cc3bcbcf299c3510e2accb": {
                "Name": "container_1",
                "EndpointID": "7e4798966484e8e02135c1b6331fc7242d1ff1499137540295ca8b503cfd734b",
                "MacAddress": "02:42:c0:a8:60:02",
                "IPv4Address": "192.168.96.2/20",
                "IPv6Address": ""
            },
            "a795090531216b94175c13b30fbf9aacdd7c9f1272d521b47f39b6d5862bcddd": {
                "Name": "container_2",
                "EndpointID": "ecb3a85bf71631becaa092df2686d435053acd8ee2f8f222e91d3494b18519fd",
                "MacAddress": "02:42:c0:a8:60:03",
                "IPv4Address": "192.168.96.3/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
  • container_2 -> container_1 ping 테스트

# container_2 attach
$ docker attach container_2
# DNS 목록 조회
/ \# cat /etc/resolv.conf 
nameserver 127.0.0.11
options ndots:0
# ping 테스트
/ \# ping container_1 -c 3
PING container_1 (192.168.96.2): 56 data bytes
64 bytes from 192.168.96.2: seq=0 ttl=64 time=0.122 ms
64 bytes from 192.168.96.2: seq=1 ttl=64 time=0.159 ms
64 bytes from 192.168.96.2: seq=2 ttl=64 time=0.139 ms

--- container_1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.122/0.140/0.159 ms
# container_1 이름에 대한 DNS resolution 확인
/ \# nslookup container_1
Server:         127.0.0.11
Address:        127.0.0.11:53

Non-authoritative answer:
Name:   container_1
Address: 192.168.96.2

Non-authoritative answer:

위 테스트처럼 사용자 정의 bridge network에 속한 container의 경우 docker 내부 DNS 서버(127.0.0.1)를 통해 container 이름을 통한 DNS resolution이 가능하다.

에외 발생 케이스는 아래와 같다.

  • 사용자 정의 bridge network 생성 및 조회(코드 생략)

  • 이름을 지정한 container_2 container와 이름을 지정하지 않은 container 생성

# 이름 명시하지 않은 container 생성
$ docker run --rm -tid --network test alpine
1b2bcded83671013f5274de2aea072e343877bd9590ad70c9e7315a865e99b1f
# 이름 조회
$ docker ps --filter id=1b2bcded83671
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS         PORTS     NAMES
1b2bcded8367   alpine    "/bin/sh"   2 minutes ago   Up 2 minutes             unruffled_stonebraker
# container_2 생성
$ docker run --rm -tid --network test --name container_2 alpine
9359704cdc048b2c4cb0d1217c9d1fd985ed3f4986114648003a3f4783e4b289
  • test bridge network에 위에서 생성한 2개의 container가 연결되어있는지 확인

$ docker network inspect test
[
    {
        "Name": "test",
        "Id": "a23414a0e7620943a4335fde5ed15c1bd9cfd41be4f81ab44b8d27f0ff268185",
        "Created": "2022-03-11T10:25:21.362459215+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.96.0/20",
                    "Gateway": "192.168.96.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1b2bcded83671013f5274de2aea072e343877bd9590ad70c9e7315a865e99b1f": {
                "Name": "unruffled_stonebraker",
                "EndpointID": "8989ea3e4a00b22a7801adc1d3d0ab87569e03a9c4a40ee94a6be09383225b3b",
                "MacAddress": "02:42:c0:a8:60:02",
                "IPv4Address": "192.168.96.2/20",
                "IPv6Address": ""
            },
            "9359704cdc048b2c4cb0d1217c9d1fd985ed3f4986114648003a3f4783e4b289": {
                "Name": "container_2",
                "EndpointID": "22ecd4e73d554a04e940200dcbc21c2e50bac6f90bd96e6618bef59a3d38ed8a",
                "MacAddress": "02:42:c0:a8:60:03",
                "IPv4Address": "192.168.96.3/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
  • container_2 -> unruffled_stonebraker ping 테스트

# container_2 attach
$ docker attach container_2
# DNS 목록 조회
/ \# cat /etc/resolv.conf 
nameserver 127.0.0.11
options ndots:0
# container 이름 ping 테스트
/ \# ping unruffled_stonebraker
ping: bad address 'unruffled_stonebraker'
# container ip ping 테스트
/ \# ping 192.168.96.2 -c 2
PING 192.168.96.2 (192.168.96.2): 56 data bytes
64 bytes from 192.168.96.2: seq=0 ttl=64 time=0.131 ms
64 bytes from 192.168.96.2: seq=1 ttl=64 time=0.101 ms

--- 192.168.96.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.101/0.116/0.131 ms
# unruffled_stonebraker 이름에 대한 DNS resolution 확인
/ \# nslookup unruffled_stonebraker
Server:         127.0.0.11
Address:        127.0.0.11:53

** server can't find unruffled_stonebraker: NXDOMAIN

** server can't find unruffled_stonebraker: NXDOMAIN

정상 케이스와 다르게 이름을 명시하지 않은 container는 DNS resolution이 되지 않는다(IP를 통한 ping 테스트는 정상).

위 테스트 과정은 아래 Docker Documentation에 더 자세히 설명한다.

Analysis

Resolution

Etc

Last updated