본문 바로가기
클라우드 컴퓨팅/Docker

도커 네트워크 기본 #2 - 네트워킹 튜토리얼(bridge)

by 안드레날린 2022. 5. 28.

도커 네트워크 드라이버 튜토리얼

default bridge network

기본 브리지 네트워크는 프로덕션 환경에 적합한 선택이 아니다.

docker run -dit --name alpine1 alpine ash
docker run -dit --name alpine2 alpine ash

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
87f41c9fb908   alpine    "ash"     7 seconds ago    Up 6 seconds              alpine2
21e9857321ae   alpine    "ash"     16 seconds ago   Up 15 seconds             alpine1

 

docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "667b030a703999ce68b3add5105bdab37d3b718d1c3d0634c7106f030c791372",
        "Created": "2022-05-27T15:59:36.9393507Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "21e9857321aec19622e653904bb6dd682141ed7dadbfcd65a911faf1e1f58899": {
                "Name": "alpine1",
                "EndpointID": "b55ad0405aebf4bc072fa7028b5482a13a1f4d08fc39339d300b730073fc3dd5",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "87f41c9fb90894177e4f641bc12394db569dcda6e1eefef0ac7b9ea742bfb241": {
                "Name": "alpine2",
                "EndpointID": "b02e2ea95ec79f4b0729f9586f1662ec644bcfc8be65045f48e559ce08c5e559",
                "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": "1500"
        },
        "Labels": {}
    }
]

 

docker exec -it alpine1 ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

docker exec -it alpine1 ping -c 3 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.056 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.042 ms
64 bytes from 172.17.0.3: seq=2 ttl=64 time=0.043 ms

docker exec -it alpine1 ping -c 3 alpine2
ping: bad address 'alpine2'



docker attach alpine1
/ # ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

/ # ping -c 3 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.039 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.132 ms
64 bytes from 172.17.0.3: seq=2 ttl=64 time=0.053 ms

/ # ping -c 3 alpine2
ping: bad address 'alpine2'

 

user-defined bridge networks

사용자 정의 브리지 네트워크는 프로덕션 환경에서 실행되는 독립 실행형 컨테이너에 권장한다.

docker network create --driver bridge alpine-net

또는

docker network create alpine-net
docker network ls
NETWORK ID     NAME         DRIVER    SCOPE
fb12f14ece6a   alpine-net   bridge    local
667b030a7039   bridge       bridge    local
b6c845f5b7ae   host         host      local
fcaada9c5dd4   none         null      local

docker network inspect alpine-net
[
    {
        "Name": "alpine-net",
        "Id": "fb12f14ece6acb79c586b487eba3b9518c798ed786e7dae1691c608e00a0317b",
        "Created": "2022-05-27T18:45:40.1642888Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

 

alpine1 컨테이너 : 사용자 정의 브리지 네트워크에 연결

alpine2 컨테이너 : 사용자 정의 브리지 네트워크에 연결

alpine3 컨테이너 : 기본 브리지 네트워크 연결

alpine4 컨테이너 : 기본 브리지 네트워크 + 사용자 정의 브리지 네트워크에 연결

docker run -dit --name alpine1 --network alpine-net alpine ash
docker run -dit --name alpine2 --network alpine-net alpine ash
docker run -dit --name alpine3 alpine ash
docker run -dit --name alpine4 --network alpine-net alpine ash
docker network connect bridge alpine4

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
136ef4eb3019   alpine    "ash"     2 minutes ago   Up 2 minutes             alpine4
97dbe150f17d   alpine    "ash"     2 minutes ago   Up 2 minutes             alpine3
51b724b8d156   alpine    "ash"     2 minutes ago   Up 2 minutes             alpine2
2497294d4a34   alpine    "ash"     2 minutes ago   Up 2 minutes             alpine1

docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "667b030a703999ce68b3add5105bdab37d3b718d1c3d0634c7106f030c791372",
        "Created": "2022-05-27T15:59:36.9393507Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "136ef4eb3019fc5c6b2d8ac8b2838541fa9913bb975b8d999b7aa72b229249f0": {
                "Name": "alpine4",
                "EndpointID": "5bd1fd419b28775d72b34ba17768bb3b56e50946a27481975a25e459e62ca91d",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "97dbe150f17d7df6eb6381f9b2b7db6caa60ef6f9a41f129fe946b43555fda04": {
                "Name": "alpine3",
                "EndpointID": "ae5966f0f758e4f8c3599206cafe6d06d267ed3cc549287b71c75a3f166d92fe",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/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": "1500"
        },
        "Labels": {}
    }
]

docker network inspect alpine-net
[
    {
        "Name": "alpine-net",
        "Id": "fb12f14ece6acb79c586b487eba3b9518c798ed786e7dae1691c608e00a0317b",
        "Created": "2022-05-27T18:45:40.1642888Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "136ef4eb3019fc5c6b2d8ac8b2838541fa9913bb975b8d999b7aa72b229249f0": {
                "Name": "alpine4",
                "EndpointID": "52ffd824fdac722570577275b891062f076c60a6d1f9d59d1829d4913afe889c",
                "MacAddress": "02:42:ac:14:00:04",
                "IPv4Address": "172.20.0.4/16",
                "IPv6Address": ""
            },
            "2497294d4a3493f046f25e8ea08f60b7c369a48491531e96d54bb90944e26cf7": {
                "Name": "alpine1",
                "EndpointID": "1bf9ff440641f1d52536a5cebc20c9e57c7f8cfa6bf8e05bd0684cd1c4f51d56",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            },
            "51b724b8d156c007168787b0aa53dff7a8e6319c311d43245066de5a8acfbfb9": {
                "Name": "alpine2",
                "EndpointID": "a77dced23515a7a6f02054239b0837ace2263023b5e76db79902e726fca220b5",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

 

사용자 정의 브리지 네트워크에서 컨테이너는 IP 주소로 통신할 수 있을 뿐만 아니라 컨테이너 이름을 IP 주소로 확인할 수도 있다. 이 기능을 '자동 서비스 검색'이라고 한다.

docker attach alpine1

/ # ping -c 3 alpine2
PING alpine2 (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.068 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.050 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.043 ms
/ # ping -c 3 172.20.0.3
PING 172.20.0.3 (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.052 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.142 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.114 ms

/ # ping -c 3 alpine3
ping: bad address 'alpine3'
/ # ping -c 3 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes

--- 172.17.0.2 ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss

/ # ping -c 3 alpine4
PING alpine4 (172.20.0.4): 56 data bytes
64 bytes from 172.20.0.4: seq=0 ttl=64 time=0.046 ms
64 bytes from 172.20.0.4: seq=1 ttl=64 time=0.046 ms
64 bytes from 172.20.0.4: seq=2 ttl=64 time=0.043 ms
/ # ping -c 3 172.20.0.4
PING 172.20.0.4 (172.20.0.4): 56 data bytes
64 bytes from 172.20.0.4: seq=0 ttl=64 time=0.051 ms
64 bytes from 172.20.0.4: seq=1 ttl=64 time=0.045 ms
64 bytes from 172.20.0.4: seq=2 ttl=64 time=0.055 ms

 

docker attach alpine4

/ # ping -c 3 alpine1
PING alpine1 (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.034 ms
64 bytes from 172.20.0.2: seq=1 ttl=64 time=0.045 ms
64 bytes from 172.20.0.2: seq=2 ttl=64 time=0.046 ms
/ # ping -c 3 172.20.0.2
PING 172.20.0.2 (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.048 ms
64 bytes from 172.20.0.2: seq=1 ttl=64 time=0.050 ms
64 bytes from 172.20.0.2: seq=2 ttl=64 time=0.046 ms

/ # ping -c 3 alpine2
PING alpine2 (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.060 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.056 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.048 ms
/ # ping -c 3 172.20.0.3
PING 172.20.0.3 (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.054 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.046 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.046 ms

/ # ping -c 3 alpine3
ping: bad address 'alpine3'
/ # ping -c 3 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.085 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.044 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.043 ms