概要

docker コンテナ同士で通信する方法を記載する。
ここでは2つのコンテナで nginx を動作させ、それぞれのコンテナからもう一方のコンテナにアクセスできるようにする。

目次

Dockerネットワークによる通信

作業用ディレクトリを作成

$ mkdir my_docker_network
$ cd my_docker_network

dockerネットワークを作成

$ docker network create my-docker-network

作成した dockerネットワークを確認

$ docker network inspect my-docker-network
[
    {
        "Name": "my-docker-network",
        "Id": "ef9813e65909c9b9cdf3bdba399b791a9c3be9240b591fc3d84c6fcf2cc28d73",
        "Created": "XXXX-XX-XXTXX:XX:XX.XXXXXXZ",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

試しに2つのコンテナを用意して双方から通信してみる。

コンテナの作成

作業用ディレクトリの作成

$ mkdir -p container_a/www
$ mkdir -p container_b/www

container_a/Dockerfile 及び container_b/Dockerfile (内容は同じ)

FROM nginx:latest

# 通信確認用にcurlを入れておく
RUN apt-get -y update
RUN apt-get -y install curl

CMD nginx -g "daemon off;"

container_a/www/index.html

<!doctype html>
<html>
<h1>Container A!</h1>
</html>

container_b/www/index.html

<!doctype html>
<html>
<h1>Container B!</h1>
</html>

コンテナのビルド/起動

以下、ビルドイメージ名は a と b で変えているが、Dockerfile の内容は同じなので別にイメージ名変える必要はない。

コンテナAのビルド 及び起動

$ docker build -t nginx-a ./container_a
$ docker run --rm --name container-a -v `pwd`/container_a/www:/usr/share/nginx/html --network my-docker-network -d nginx-a

コンテナBのビルド 及び起動

$ docker build -t nginx-b ./container_b
$ docker run --rm --name container-b -v `pwd`/container_b/www:/usr/share/nginx/html --network my-docker-network -d nginx-b

コンテナに入って通信確認

コンテナAに入る。

$ docker exec -it container-a /bin/bash

自身(コンテナA)にリクエストを飛ばしてみる。

# curl http://localhost

<!doctype html>
<html>
<h1>Container A!</h1>
</html>

自身(コンテナA)にコンテナ名でリクエストを飛ばしてみる。

# curl http://container-a

<!doctype html>
<html>
<h1>Container A!</h1>
</html>

コンテナBにリクエストを飛ばしてみる。

# curl http://container-b

<!doctype html>
<html>
<h1>Container B!</h1>
</html>

docker-composeで行う場合

上記で作成した Dockerfile をそのまま利用する形で docker-compose を書いておけばもっと楽。

docker-compose.yml の作成

docker-compose.yml

version: "3" 

services:
  webserver1:
    build:
      context: ./container_a
    container_name: container-a
    hostname: container-a
    volumes:
      - ./container_a/www:/usr/share/nginx/html
    ports:
      - "8080:80"
    command: /bin/sh -c "nginx -g 'daemon off;'"
  
  webserver2:
    build:
      context: ./container_b
    container_name: container-b
    hostname: container-b
    volumes:
      - ./container_b/www:/usr/share/nginx/html
    ports:
      - "8081:80"
    command: /bin/sh -c "nginx -g 'daemon off;'"

ビルド

$ docker-compose up --build --no-start

Creating network "my_docker_network_default" with the default driver           # [プロジェクト名]_default という名前で自動的にネットワークが作成される
Building webserver1
Step 1/4 : FROM nginx:latest
 ---> 7042885a156a
Step 2/4 : RUN apt-get -y update
 ---> Using cache
 ---> 0ea23b5549be
Step 3/4 : RUN apt-get -y install curl
 ---> Using cache
 ---> 3462f4bb1172
Step 4/4 : CMD nginx -g "daemon off;"
 ---> Using cache
 ---> 5e1fc3b98d4f
Successfully built 5e1fc3b98d4f
Successfully tagged my_docker_network_webserver1:latest
Building webserver2
Step 1/4 : FROM nginx:latest
 ---> 7042885a156a
Step 2/4 : RUN apt-get -y update
 ---> Using cache
 ---> 0ea23b5549be
Step 3/4 : RUN apt-get -y install curl
 ---> Using cache
 ---> 3462f4bb1172
Step 4/4 : CMD nginx -g "daemon off;"
 ---> Using cache
 ---> 5e1fc3b98d4f
Successfully built 5e1fc3b98d4f
Successfully tagged my_docker_network_webserver2:latest
Creating container-b ... done
Creating container-a ... done

起動

$ docker-compose start
Starting webserver1 ... done
Starting webserver2 ... done

コンテナ間の通信を確認

コンテナAからコンテナBへの通信を確認

$ docker exec container-a curl -s http://container-b
<!doctype html>
<html>
<h1>Container B!</h1>
</html>

コンテナBからコンテナAへの通信を確認

$ docker exec container-b curl -s http://container-a
<!doctype html>
<html>
<h1>Container A!</h1>
</html>

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-12-19 (水) 00:55:42 (1955d)