반응형

container to host 통신 예제

docker command을 활용한 예제

docker run -dit --add-host host.docker.internal:host-gateway --name alpine1 alpine
docker exec -it alpine1 sh
ping -c 3 host.docker.internal

docker-compose를 활용한 예제

version: "3.3"
services:
  container1:
    image: alpine
    extra_hosts:
      - host.docker.internal:host-gateway
    command: ping -c 3 host.docker.internal
docker-compose up

container to container 통신 예제

docker command를 활용한 예제

docker network create --driver bridge alpine-net
docker run -dit --network alpine-net --name alpine1 alpine
docker run -dit --network alpine-net --name alpine2 alpine
docker exec -it alpine1 sh
ping -c 3 alpine2

docker-compose를 활용한 예제

version: "3.3"
services:
  container1:
    image: alpine
    depends_on:
      - container2
    command: ping -c 3 container2
  container2:
    image: alpine
    command: sleep 5s
docker-compose up

docker-compose를 활용한 예제 - 미리 생성된 network 사용

version: "3.3"
services:
  container1:
    container_name: alpine1
    image: alpine
    command: tail -f /dev/null
    networks:
      - alpine-net
networks:
  alpine-net:
    external: true
version: "3.3"
services:
  container2:
    container_name: alpine2
    image: alpine
    command: ping -c 3 alpine1 # 'ping -c 3 container1'도 가능
    networks:
      - alpine-net
networks:
  alpine-net:
    external: true

기타

network 목록 확인

docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
9c2d1cd41147   alpine-net        bridge    local

container가 network에 포함되어 있는지 확인

 docker network inspect alpine-net
[
        ...
        "Containers": {
            "d42c4f0e3d820a621719dc0b8dc2d6b99095be6ad6053ae6d6ea2089a429a81c": {
                "Name": "alpine2",
                "EndpointID": "90d26511d60b903a497d917d110ef832d0bc962163fd34dab2548075f2ecd88d",
                "MacAddress": "02:42:ac:16:00:03",
                "IPv4Address": "172.22.0.3/16",
                "IPv6Address": ""
            },
            "dc59405d51f97f4a09eb957e6bc82e13aae241e5644d08db36ebaf7b494287af": {
                "Name": "alpine1",
                "EndpointID": "a0c1a3699cbc92c0e70b8a4aa3347448c00734edc68b0f153c88ee102f2663ed",
                "MacAddress": "02:42:ac:16:00:02",
                "IPv4Address": "172.22.0.2/16",
                "IPv6Address": ""
            }
        }
    }
]

참고

반응형

+ Recent posts