65
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker コンテナ間の通信

Last updated at Posted at 2014-04-07

--icc オプションに false を指定して Docker を立ち上げた場合に、コンテナ間の通信を許可するためには docker run の実行時に --link オプションを指定する必要があります

この状態で、コンテナ間の通信の設定をおこない、実際に通信できるかを確認してみました

icc (inter-container communication) の無効化

--icc オプションはデフォルトで true が設定されています

--icc=false を指定するために、/etc/default/docker に下記を追記して docker を再起動します

DOCKER_OPTS="--icc=false ${DOCKER_OPTS}"

コンテナの作成

2 つのコンテナに Apache をインストールして、他のコンテナから HTTP リクエストを送ってみます

まずは、Apache をインストールしたコンテナをコミットしておきます

$ docker run -d ubuntu:13.10 /bin/sh -c 'apt-get -y update && apt-get -y install apache2 && a2enmod ssl && a2ensite default-ssl'
d0f34aef58df5c166b23cf69974d72d97e981a92cfc448ef527023fb24ee3ed0
$ docker commit d0f34aef58df5c166b23cf69974d72d97e981a92cfc448ef527023fb24ee3ed0 hexa/base
676c043b591d158ad003a1b0265b214c4f0f4035944225cfaa9d72b6ed9b1580

Apache 用コンテナ

コンテナを 2 つ作成して Apache を立ち上げます
また、別コンテナからの HTTP リクエストの受信を確認するために tail -f access.log を実行しておきます

foo1 コンテナ

$ docker run -d -t --name foo1 -p 0.0.0.0:50080:80 -p 0.0.0.0:50443:443 hexa/base /bin/sh -c '/etc/init.d/apache2 start && tail -f /var/log/apache2/access.log'
f13e17260f7fce4633183a0436b4eca8f4021a6383e6615ce7f34f44e42ee359
$ docker attach foo1

foo2 コンテナ

$ docker run -d -t --name foo2 -p 0.0.0.0:60080:80 -p 0.0.0.0:60443:443 hexa/base /bin/sh -c '/etc/init.d/apache2 start && tail -f /var/log/apache2/access.log'
a5273956c024a81253ca9de917a2ffbda4ad9e0d7e08cbc2861369fcd0e252ca
$ docker attach foo2

起動中のコンテナの確認

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                           NAMES
a5273956c024        hexa/base:latest    /bin/sh -c /etc/init   19 seconds ago      Up 18 seconds       0.0.0.0:60443->443/tcp, 0.0.0.0:60080->80/tcp   foo2
f13e17260f7f        hexa/base:latest    /bin/sh -c /etc/init   44 seconds ago      Up 43 seconds       0.0.0.0:50443->443/tcp, 0.0.0.0:50080->80/tcp   foo1

リクエスト送信用コンテナ

docker run の --link オプションに、リンクさせるコンテナの名前を指定します

--link オプションは name:alias で指定します

今回は name に事前に立ちあげておいた foo1 と foo2 を指定します

$ docker run -i -t --name bar --link foo1:apache1 --link foo2:apache2  ubuntu:13.10 /bin/bash

立ち上げたコンテナの環境変数を確認します

root@11027ab6adfa:/# env | grep APACHE
APACHE2_PORT_443_TCP=tcp://172.17.0.3:443
APACHE2_PORT_80_TCP_PORT=80
APACHE1_PORT_443_TCP=tcp://172.17.0.2:443
APACHE1_PORT=tcp://172.17.0.2:80
APACHE2_PORT_443_TCP_PORT=443
APACHE2_PORT_80_TCP_PROTO=tcp
APACHE2_PORT_443_TCP_PROTO=tcp
APACHE1_PORT_80_TCP_PORT=80
APACHE1_PORT_80_TCP_PROTO=tcp
APACHE2_NAME=/bar/apache2
APACHE2_PORT=tcp://172.17.0.3:80
APACHE1_PORT_443_TCP_PROTO=tcp
APACHE1_NAME=/bar/apache1
APACHE2_PORT_80_TCP=tcp://172.17.0.3:80
APACHE1_PORT_443_TCP_ADDR=172.17.0.2
APACHE1_PORT_80_TCP_ADDR=172.17.0.2
APACHE1_PORT_443_TCP_PORT=443
APACHE1_PORT_80_TCP=tcp://172.17.0.2:80
APACHE2_PORT_80_TCP_ADDR=172.17.0.3
APACHE2_PORT_443_TCP_ADDR=172.17.0.3

foo1 コンテナと foo2 コンテナの IP アドレスと、docker run -p で指定した公開しているポート番号が設定されています

iptables

確認のために iptables も見ておきます

$ sudo iptables -L -v
Chain INPUT (policy ACCEPT 13 packets, 808 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.3           172.17.0.4           tcp spt:http
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.4           172.17.0.3           tcp dpt:http
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.3           172.17.0.4           tcp spt:https
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.4           172.17.0.3           tcp dpt:https
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.2           172.17.0.4           tcp spt:http
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.4           172.17.0.2           tcp dpt:http
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.2           172.17.0.4           tcp spt:https
    0     0 ACCEPT     tcp  --  docker0 docker0  172.17.0.4           172.17.0.2           tcp dpt:https
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.2           tcp dpt:http
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.2           tcp dpt:https
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.3           tcp dpt:http
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.3           tcp dpt:https
   11   732 DROP       all  --  docker0 docker0  anywhere             anywhere
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.3           tcp dpt:http
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.3           tcp dpt:https
   12  1582 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.2           tcp dpt:http
    0     0 ACCEPT     tcp  --  !docker0 docker0  anywhere             172.17.0.2           tcp dpt:https
40466   88M ACCEPT     all  --  any    docker0  anywhere             anywhere             ctstate RELATED,ESTABLISHED
21506  990K ACCEPT     all  --  docker0 !docker0  anywhere             anywhere

Chain OUTPUT (policy ACCEPT 7 packets, 712 bytes)
 pkts bytes target     prot opt in     out     source               destination

foo1 コンテナ, foo2 コンテナにリクエストを送る

curl をインストールして、リクエストを送ります

# apt-get -y update && apt-get -y install curl
# curl -i -X GET http://$APACHE1_PORT_80_TCP_ADDR:$APACHE1_PORT_80_TCP_PORT/
# curl -i -X GET --insecure https://$APACHE1_PORT_443_TCP_ADDR:$APACHE1_PORT_443_TCP_PORT/
# curl -i -X GET http://$APACHE2_PORT_80_TCP_ADDR:$APACHE2_PORT_80_TCP_PORT/
# curl -i -X GET --insecure https://$APACHE2_PORT_443_TCP_ADDR:$APACHE2_PORT_443_TCP_PORT/

foo1 コンテナへのリクエスト

root@11027ab6adfa:/# curl -i -X GET http://$APACHE1_PORT_80_TCP_ADDR:$APACHE1_PORT_80_TCP_PORT/
HTTP/1.1 200 OK
Date: Sun, 06 Apr 2014 10:47:30 GMT
Server: Apache/2.4.6 (Ubuntu)
Last-Modified: Sun, 06 Apr 2014 10:41:33 GMT
ETag: "b1-4f65d64f4d140"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
root@11027ab6adfa:/# curl -i -X GET --insecure https://$APACHE1_PORT_443_TCP_ADDR:$APACHE1_PORT_443_TCP_PORT/
HTTP/1.1 200 OK
Date: Sun, 06 Apr 2014 10:47:33 GMT
Server: Apache/2.4.6 (Ubuntu)
Last-Modified: Sun, 06 Apr 2014 10:41:33 GMT
ETag: "b1-4f65d64f4d140"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

foo2 コンテナへのリクエスト

root@11027ab6adfa:/# curl -i -X GET http://$APACHE2_PORT_80_TCP_ADDR:$APACHE2_PORT_80_TCP_PORT/
HTTP/1.1 200 OK
Date: Sun, 06 Apr 2014 10:50:13 GMT
Server: Apache/2.4.6 (Ubuntu)
Last-Modified: Sun, 06 Apr 2014 10:41:33 GMT
ETag: "b1-4f65d64f4d140"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
root@11027ab6adfa:/# curl -i -X GET --insecure https://$APACHE2_PORT_443_TCP_ADDR:$APACHE2_PORT_443_TCP_PORT/
HTTP/1.1 200 OK
Date: Sun, 06 Apr 2014 10:50:21 GMT
Server: Apache/2.4.6 (Ubuntu)
Last-Modified: Sun, 06 Apr 2014 10:41:33 GMT
ETag: "b1-4f65d64f4d140"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

ログの確認

foo1 コンテナ の /var/log/apach2/access.log

$ docker attach foo1
172.17.0.4 - - [06/Apr/2014:10:47:30 +0000] "GET / HTTP/1.1" 200 427 "-" "curl/7.32.0"
172.17.0.4 - - [06/Apr/2014:10:47:33 +0000] "GET / HTTP/1.1" 200 1762 "-" "curl/7.32.0"

foo2 コンテナ の /var/log/apach2/access.log

$ docker attach foo2
172.17.0.4 - - [06/Apr/2014:10:50:13 +0000] "GET / HTTP/1.1" 200 427 "-" "curl/7.32.0"
172.17.0.4 - - [06/Apr/2014:10:50:21 +0000] "GET / HTTP/1.1" 200 1762 "-" "curl/7.32.0"

参考

65
64
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?