Docker Compose を用いた複数コンテナの連携管理
Docker Compose とは複数のコンテナを効率的に管理できるようになるツール。
具体的には、サービスを構成するコンテナの実行に必要な情報を記述した設定ファイルを参照することで、サービスを構成するコンテナ群をまとめて起動、停止させたり、コンテナ群の実行状態を調べたりという作業を可能にする。
Docker Compose のインストール
$ curl -L https://github.com/docker/compose/releases/download/1.6.2/docker-compose-`uname -s`-`uname -m` > /usr/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 838 0 --:--:-- --:--:-- --:--:-- 838
100 7743k 100 7743k 0 0 1216k 0 0:00:06 0:00:06 --:--:-- 1669k
$ chmod +x /usr/bin/docker-compose
$ sudo docker-compose --version
docker-compose version 1.6.2, build 4d72027
WordPress と MySQL のイメージを連携
以下のファイルを作成。
$ cat docker-compose.yml
wordpress:
image: wordpress
links:
- dbms:mysql
ports:
- 8080:80
dbms:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=password
上記の設定ファイルがあるディレクトリで以下のコマンドを実行。
$ sudo docker-compose up -d
Pulling dbms (mysql:latest)...
latest: Pulling from library/mysql
85b1f47fba49: Already exists
27dc53f13a11: Pull complete
095c8ae4182d: Pull complete
0972f6b9a7de: Pull complete
1b199048e1da: Pull complete
159de3cf101e: Pull complete
2ff639f4e6d9: Pull complete
2fc3747285af: Pull complete
7e53dd019982: Pull complete
6c1d6c94512c: Pull complete
Digest: sha256:9ed76f990ff4e6996827148b829bf823e96388da4009f1b8bf8d01691ca93113
Status: Downloaded newer image for mysql:latest
Creating wordpress_dbms_1
Pulling wordpress (wordpress:latest)...
latest: Pulling from library/wordpress
85b1f47fba49: Already exists
66e22dddbf92: Pull complete
bf0df491fd2e: Pull complete
425226034711: Pull complete
5ba5c1815115: Pull complete
98ab63dc099c: Pull complete
389f9436e431: Pull complete
d5bc131d410e: Pull complete
457ec74054f2: Pull complete
bff1a33a03f7: Pull complete
8e6802925a83: Pull complete
70b2639141e1: Pull complete
2b02b6464099: Pull complete
8bf2a146ddc0: Pull complete
73cc7d14c1ed: Pull complete
d2b2ef7e5218: Pull complete
6dd6734484e2: Pull complete
725e747b6a51: Pull complete
Digest: sha256:dd55a177a6c28825ab406f6396ba2fdef017b654f49dcb510bc2def8892f2070
Status: Downloaded newer image for wordpress:latest
Creating wordpress_wordpress_1
[ホストの IP アドレス]:8080 にブラウザでアクセスすると以下のページが表示される。
kibana の例
docker-compose.yml は以下。
$ cat docker-compose.yml
kibana:
image: kibana
ports:
- "5601:5601"
links:
- elasticsearch
elasticsearch:
image: elasticsearch
ports:
- "9200:9200"
logstash:
image: logstash
command: logstash -f /etc/logstash/conf.d/logstash.conf
volumes:
- /etc/logstash/conf.d:/etc/logstash/conf.d
ports:
- "5000:5000"
links:
- elasticsearch
logstash の設定ファイルは以下。
$ cat /etc/logstash/conf.d/logstash.conf
input {
tcp {
port => 5000
}
}
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
geoip {
source => "clientip"
}
}
output {
elasticsearch {
hosts => "elasticsearch:9200"
}
}
サービスの起動。
$ sudo docker-compose up -d
Pulling elasticsearch (elasticsearch:latest)...
latest: Pulling from library/elasticsearch
3e17c6eae66c: Pull complete
74d44b20f851: Pull complete
a156217f3fa4: Pull complete
8bceb9f57693: Pull complete
268410abd9cf: Pull complete
c5e2fb2d87cf: Pull complete
09681f714a7b: Pull complete
c20de950ce52: Pull complete
a67da81c7539: Pull complete
a85a015301f2: Pull complete
0b99f45d3830: Pull complete
85d9f5153e37: Pull complete
04f4eddfc63d: Pull complete
d642fb07a7c5: Pull complete
a040dcbfdfda: Pull complete
Digest: sha256:600d4557b64ab8a1c6b50d4224d4bb67a50c5901de5170ab1aa6194cb1e4f460
Status: Downloaded newer image for elasticsearch:latest
Creating kibana_elasticsearch_1
Pulling logstash (logstash:latest)...
latest: Pulling from library/logstash
3e17c6eae66c: Already exists
74d44b20f851: Already exists
a156217f3fa4: Already exists
8bceb9f57693: Already exists
268410abd9cf: Already exists
c5e2fb2d87cf: Already exists
09681f714a7b: Already exists
c20de950ce52: Already exists
bd7936dd0373: Pull complete
33a159940c96: Pull complete
468873f48321: Pull complete
7de9f6090f0e: Pull complete
0572d02f512d: Pull complete
7fefa054d734: Pull complete
919c5a17a974: Pull complete
2e9c411d967d: Pull complete
Digest: sha256:87801c3150c23dc7104cfa6c8205972c5c16367dcd11154b647b6ff94e766445
Status: Downloaded newer image for logstash:latest
Creating kibana_logstash_1
Pulling kibana (kibana:latest)...
latest: Pulling from library/kibana
85b1f47fba49: Already exists
5aac72d6286b: Pull complete
cb43d762a5af: Pull complete
21c9afc36893: Pull complete
f58f4ce28c4f: Pull complete
f84921a80c09: Pull complete
3e3e437895c9: Pull complete
1195449c2525: Pull complete
245cd97663ca: Pull complete
Digest: sha256:3d7c13da424e806a6cb9da14953b533c0f57b7814275a94c9b181eb159d2c5fd
Status: Downloaded newer image for kibana:latest
Creating kibana_kibana_1
[ホストの IP アドレス]:5601 にブラウザでアクセス。