- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-12-20T11:11:44+00:00","","")
#mynavi(dockerメモ)
#setlinebreak(on);
* 概要 [#fbcf9482]
#html(<div class="pl10">)
docker コンテナ同士で通信する方法を記載する。
ここでは2つのコンテナで nginx を動作させ、それぞれのコンテナからもう一方のコンテナにアクセスできるようにする。
#html(</div>)
* 目次 [#ue9f4bb8]
#contents
- 関連
-- [[dockerメモ]]
-- [[dockerでサクッとNginx環境構築]]
-- [[Nginx+uwsgi+Flaskをdockerで動かす]]
-- [[LAMP環境のdockerイメージを作る]]
- 参考
-- https://docs.docker.com/compose/networking/
-- http://docs.docker.jp/compose/networking.html
* Dockerネットワークによる通信 [#a903cfc0]
#html(<div class="pl10">)
作業用ディレクトリを作成
#myterm2(){{
$ mkdir my_docker_network
$ cd my_docker_network
}}
dockerネットワークを作成
#myterm2(){{
$ docker network create my-docker-network
}}
作成した dockerネットワークを確認
#myterm2(){{
$ 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つのコンテナを用意して双方から通信してみる。
** コンテナの作成 [#v728d265]
#html(<div class="pl10">)
作業用ディレクトリの作成
#myterm2(){{
$ mkdir -p container_a/www
$ mkdir -p container_b/www
}}
container_a/Dockerfile 及び container_b/Dockerfile (内容は同じ)
#mycode2(){{
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
#mycode2(){{
<!doctype html>
<html>
<h1>Container A!</h1>
</html>
}}
container_b/www/index.html
#mycode2(){{
<!doctype html>
<html>
<h1>Container B!</h1>
</html>
}}
#html(</div>)
** コンテナのビルド/起動 [#g3eccecf]
#html(<div class="pl10">)
以下、ビルドイメージ名は a と b で変えているが、Dockerfile の内容は同じなので別にイメージ名変える必要はない。
コンテナAのビルド 及び起動
#myterm2(){{
$ 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のビルド 及び起動
#myterm2(){{
$ 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
}}
#html(</div>)
** コンテナに入って通信確認 [#o6fac846]
#html(<div class="pl10">)
コンテナAに入る。
#myterm2(){{
$ docker exec -it container-a /bin/bash
}}
自身(コンテナA)にリクエストを飛ばしてみる。
#myterm2(){{
# curl http://localhost
<!doctype html>
<html>
<h1>Container A!</h1>
</html>
}}
自身(コンテナA)にコンテナ名でリクエストを飛ばしてみる。
#myterm2(){{
# curl http://container-a
<!doctype html>
<html>
<h1>Container A!</h1>
</html>
}}
コンテナBにリクエストを飛ばしてみる。
#myterm2(){{
# curl http://container-b
<!doctype html>
<html>
<h1>Container B!</h1>
</html>
}}
#html(</div>)
#html(</div>)
* docker-composeで行う場合 [#k9f5c099]
#html(<div class="pl10">)
上記で作成した Dockerfile をそのまま利用する形で docker-compose を書いておけばもっと楽。
- [ディレクトリ名]_default という名前で docker ネットワークが自動的に作成される。
- 既存のネットワークを使用する事も可能
http://docs.docker.jp/compose/networking.html#using-a-pre-existing-network
** docker-compose.yml の作成 [#z1991c96]
#html(<div class="pl10">)
docker-compose.yml
#mycode2(){{
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;'"
}}
#html(</div>)
** ビルド [#b769be83]
#html(<div class="pl10">)
#myterm2(){{
$ 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
}}
#html(</div>)
** 起動 [#w84d76d0]
#html(<div class="pl10">)
#myterm2(){{
$ docker-compose start
Starting webserver1 ... done
Starting webserver2 ... done
}}
#html(</div>)
** コンテナ間の通信を確認 [#b7da7d74]
コンテナAからコンテナBへの通信を確認
#myterm2(){{
$ docker exec container-a curl -s http://container-b
<!doctype html>
<html>
<h1>Container B!</h1>
</html>
}}
コンテナBからコンテナAへの通信を確認
#myterm2(){{
$ docker exec container-b curl -s http://container-a
<!doctype html>
<html>
<h1>Container A!</h1>
</html>
}}
#html(</div>)
#html(</div>)