공부합시다!/Container

Docker : Network --link

간서치 2022. 6. 1. 14:49
728x90

Container간의 통신에 사용되는 --link옵션에 대해서 알아봅니다.

 

1. 필요성

 1.1. Container는 IP를 동적(DHCP)으로 받아오기 때문에 항상 동일한 IP를 사용한다는 보장이 없다.

 1.2. 서로 다른 두 개 이상의 Container 통신에서 문제가 발생할 수 있음.

 1.3. link는 Container의 IP가 변경되어도 통신이 유지되도록 하는 기능.

 1.4. Container 내부의 /etc/hosts에 기록

 1.5. Container의 이름에 따른 IP가 변경되어도 추적 가능

 1.6. Container의 이름이 변경되는 경우에는 추적 불가

 

2. 실습

 2.1. Centos:7 Images로 C1 Container 생성

 2.2. Centos:7 Images로 C2 Container 생성 시 --link C1 지정

 2.3. C1 Container stop

 2.4. Centos:7 Images로 C3 Container 생성 

 2.5. C1 Container start 

 2.6. exec를 이용하여 C2 Container에서 C1 Container로 ping Test

 

3. Code

#! /bin/bash
docker run -itd --name c1 centos:7
docker inspect c1
docker run -itd --name c2 --link c1:babo1 centos:7
docker exec c2 ping -c 4 c1
docker stop c1
docker run -itd --name c3 centos:7
docker inspect c3
docker start c1
docker inspect c1
docker exec c2 ping -c 4 c1

4. 결과

c1 ping test

 

restart c1 ping test

 

# --add-host babo:10.0.0.1

# --dns 10.0.0.1

# --expose

# --hostname

# --publish, -p  hostport:containerport

# --publish-all, -P

728x90