IT/Kubernetes, Docker

docker - network

울티마 2022. 6. 14. 13:34

1. 구분

  • Host : 컨테이너를 호스트 컴퓨터와 동일한 네트워크에서 돌리기 위해 사용
  • Bridge : 하나의 호스트 내에서 여러 컨테이너들 통신 지원
  • Overlay : 여러 호스트에 분산되어 돌아가는 컨테이너들 통신 지원

 

2. Bridge 네트워크 생성

# docker network create in-single-host

89d3d3c571bf27bfc8f770692d46bf59db0d7c47c773f8efc1d01331e5ee2134

 

# docker network list

NETWORK ID     NAME             DRIVER    SCOPE

12dbbdd4baba   bridge           bridge    local

f8004ec27c55   host             host      local

89d3d3c571bf   in-single-host   bridge    local

e2e98bcf6f9e   none             null      local

 

# docker inspect network in-single-host

        "Scope": "local",

        "Driver": "bridge",

                    "Subnet": "172.18.0.0/16",

                    "Gateway": "172.18.0.1"

 

3. 컨테이너 2 생성 네트워크 연결

# docker image list

REPOSITORY   TAG               IMAGE ID       CREATED       SIZE

busybox      latest            1a80408de790   5 weeks ago   1.24MB

 

# docker run -itd --name busybox01 busybox

# docker run -itd --name busybox02 busybox

 

# docker network connect in-single-host busybox01

# docker network connect in-single-host busybox02

 

# docker network inspect in-single-host

        "Name": "in-single-host",

        "Scope": "local",

        "Driver": "bridge",

                    "Subnet": "172.18.0.0/16",

                    "Gateway": "172.18.0.1"

 

                "Name": "busybox02",

                "IPv4Address": "172.18.0.5/16",

 

                "Name": "busybox01",

                "IPv4Address": "172.18.0.4/16",

 

4. 통신 확인

# docker exec busybox01 ping busybox02   >> busybox01에서 "ping busybox02" 명령 수행

PING busybox02 (172.18.0.5): 56 data bytes

64 bytes from 172.18.0.5: seq=0 ttl=64 time=0.132 ms

64 bytes from 172.18.0.5: seq=1 ttl=64 time=0.088 ms

 

# docker exec busybox02 ping busybox01   >> busybox02에서 "ping busybox02" 명령 수행

PING busybox01 (172.18.0.4): 56 data bytes

64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.119 ms

64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.117 ms

 

5. disconnect 삭제

# docker network disconnect in-single-host busybox01

# docker network disconnect in-single-host busybox02

 

# docker network rm in-single-host

in-single-host

 

6. 컨테이너와의 연결이 하나도 없는 네트워크 제거

# docker network prune

WARNING! This will remove all custom networks not used by at least one container.

Are you sure you want to continue? [y/N] y