Docker Compose とは何なのか気になりますか?
Docker は、DevOps の世界で最も人気のあるコンテナ化ツールです。しかし、Docker Compose とは何でしょうか?
Docker Compose は、YAML ファイルを使用して複数のコンテナーを持つアプリケーションを実行するために使用されます。
Docker アプリケーションが異なるテクノロジー スタックに対して複数のコンテナーを実行する必要がある場合がいくつかあります。現在、コンテナごとに個別の dockerfile を構築、実行、接続するのは難しい作業になる可能性があります。ここで docker-compose が役に立ちます。
単一の簡単な docker-compose.yml ファイルを使用すると、単一のコマンドを実行するだけですべてのコンテナーを構築、接続、起動できます。これは、複数のアプリケーションがコンテナ内で実行される運用環境のエンタープライズ アプリケーションにとって非常に便利です。 Docker コンテナ内で数百のアプリケーションを簡単に実行できるため、時間を大幅に節約できます。

Docker Compose のインストール
Compose をインストールする前に、Docker がシステムにすでにインストールされている必要があります。
以下のコマンドを実行して docker-compose をインストールします。
@:/home/$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[sudo] password for :
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 1209 0 --:--:-- --:--:-- --:--:-- 1209
100 11.1M 100 11.1M 0 0 348k 0 0:00:32 0:00:32 --:--:-- 476k
以下のコマンドを実行してファイルのアクセス許可を設定します。
@:/home/$ sudo chmod +x /usr/local/bin/docker-compose
正しくインストールされたかどうかを確認すると、docker-compose のバージョンが返されるはずです。
@:/home/$ docker-compose --version
docker-compose version 1.23.1, build b02f1306
以下は、docker-compose で使用できるコマンドのリストです。
@:/home/$ docker-compose
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>…] [options] [COMMAND] [ARGS…]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by –tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don’t check the daemon’s hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information

Docker Compose ファイル
これは、すべての魔法を実行するサンプル docker-compose ファイルです。
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
このファイルの最初の行は、使用されているバージョンを指定します。この数値は、システムにインストールされている Docker エンジンによって異なります。 docker-compose のバージョン 3 に属する Docker 18.09.6 がインストールされています。バージョンの詳細については、こちらをご覧ください – https://docs.docker.com/compose/compose-file/compose-versioning/
この Docker ファイルは、Web と Redis という 2 つのサービス/アプリケーションを実行しています。 Web サービスは dockerfile を通じて構築され、デフォルトの flask Web サーバー ポート – 5000 で実行されます。Redis サービスは、Docker Hub レジストリから Redis イメージをプルすることによって実行されます。
docker-compose.yml ファイルを実行するには、非常に単純なコマンド docker-compose up を実行する必要があります。

Docker Compose ワークフロー
以下に、docker-compose を使用するための 3 つの手順を示します。
- 各サービスのdockerfileを作成する
- すべての dockerfile を接続するための docker-compose.yml ファイルを作成します。
- docker-compose up コマンドを実行してシステムを起動します
先ほど見たサンプル docker-compose ファイルの例を取り上げ、プロジェクト構造がどのように作成されるかを示します。
my-app
|-----web
|---------Dockerfile
|-----redis
|docker-compose.yml
my-app は私のメインプロジェクトディレクトリです。このディレクトリには、Web および Redis サービス ディレクトリと docker-compose YAML ファイルが含まれています。 WebサービスのDockerfileはWebディレクトリに存在します。 Redis サービスは Docker Hub から直接プルされるため、Redis ディレクトリに dockerfile は必要ありません。 docker-compose ワークフローは次のようになります。
Docker Compose を使用した MEAN スタック アプリケーションのコンテナ化
基本的な概念は理解できました。 docker-compose を使用して MEAN スタック アプリケーションをコンテナ化する方法のデモを示します。
MEAN は、MongoDB、Express、Angular、Node.js の略です。これらのサービスを組み合わせて使用するアプリケーションは、MEAN/フルスタック アプリケーションとも呼ばれます。
このデモでは、3 つの Docker コンテナを実行します。
- コンテナ 1 – 角度のある
- コンテナ 2 – NodeJS と ExpressJS
- コンテナ 3 – MongoDB
完全なアプリケーションをここからダウンロードします: http://bit.ly/2St7r3A (本番環境ではテストされていません)
これら 3 つのコンテナを実行する場合の docker-compose.yml ファイルは次のようになります。
version: '3'
services:
angular:
build: angular-client
ports:
- "4200:4200"
volumes:
- ./angular-client/:/var/www/app
express:
build: express-server
ports:
- "3000:3000"
volumes:
- ./express-server/:/var/www/app
links:
- database
database:
image: mongo
ports:
- "27017:27017"
- 最初の行は、使用されている docker-compose のバージョンを指定します。
- 私たちは 3 つのサービス (Angular、Express、データベース) を実行しています。
- Angular サービスは dockerfile を使用して構築されます。ポート 4200 で実行され、アプリケーション ボリュームは /var/www/app です。
- Express サービスは dockerfile を使用して構築されます。 Express サーバーはポート 3000 で実行され、ボリュームは /var/www/app です。
- データベース サービスは、dockerhub から MongoDB イメージをプルし、27017 以降に開始します。
ホーム ディレクトリでプロジェクトを抽出し、angular-client ディレクトリに移動します。
@:~$ cd mean
@:~/mean$ cd angular-client
システムにノード パッケージ マネージャー (npm) がインストールされていない場合は、以下のコマンドを実行します (インストールされている場合は無視してください)。
@:~/mean/angular-client$ sudo apt install npm
[sudo] password for :
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
gyp javascript-common libc-ares2 libhttp-parser2.8 libjs-async libjs-inherits libjs-is-typedarray
libjs-jquery libjs-node-uuid libjs-underscore libssl1.0-dev libuv1 libuv1-dev node-abbrev node-ajv
node-ansi node-ansi-color-table node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archy
node-are-we-there-yet node-async node-aws-sign2 node-balanced-match node-block-stream node-bluebird
node-boom node-brace-expansion node-builtin-modules node-camelcase node-caseless node-chalk node-cliui
node-clone node-co node-color-convert node-color-name node-combined-stream node-concat-map
node-config-chain node-console-control-strings node-cookie-jar node-copy-concurrently node-core-util-is
node-yallist node-yargs node-yargs-parser nodejs nodejs-dev nodejs-doc
Suggested packages:
apache2 | lighttpd | httpd node-aws-sign node-oauth-sign node-http-signature debhelper
The following NEW packages will be installed:
gyp javascript-common libc-ares2 libhttp-parser2.8 libjs-async libjs-inherits libjs-is-typedarray
libjs-jquery libjs-node-uuid libjs-underscore libssl1.0-dev libuv1 libuv1-dev node-abbrev node-ajv
node-ansi node-ansi-color-table node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archy
node-are-we-there-yet node-async node-aws-sign2 node-balanced-match node-block-stream node-bluebird
0 upgraded, 212 newly installed, 0 to remove and 233 not upgraded.
Need to get 10.5 MB of archives.
After this operation, 53.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 gyp all 0.1+20180428git4d467626-1 [237 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 javascript-common all 11 [6,066 B]
Get:3 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 libhttp-parser2.8 amd64 2.8.1-1 [20.8 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libjs-async all 0.8.0-3 [25.4 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libjs-is-typedarray all 1.0.0-2 [2,934 B]
Get:6 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 libjs-jquery all 3.2.1-1 [152 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libjs-node-uuid all 1.4.7-5 [11.5 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 libjs-underscore all 1.8.3~dfsg-1 [59.9 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu cosmic-updates/main amd64 libssl1.0-dev amd64 1.0.2n-1ubuntu6.2 [1,366 kB]
Fetched 10.5 MB in 1min 34s (112 kB/s)
Extracting templates from packages: 100%
Selecting previously unselected package gyp.
(Reading database ... 180130 files and directories currently installed.)
Preparing to unpack .../000-gyp_0.1+20180428git4d467626-1_all.deb ...
Unpacking gyp (0.1+20180428git4d467626-1) ...
Selecting previously unselected package javascript-common.
Preparing to unpack .../001-javascript-common_11_all.deb ...
Unpacking javascript-common (11) ...
Selecting previously unselected package libhttp-parser2.8:amd64.
Preparing to unpack .../002-libhttp-parser2.8_2.8.1-1_amd64.deb ...
Setting up node-fstream-ignore (0.0.6-2) ...
Setting up node-gyp (3.6.2-2) ...
Setting up node-yargs (10.0.3-2) ...
Setting up npm (5.8.0+ds-2) ...
Processing triggers for libc-bin (2.28-0ubuntu1) ...
angular-client ディレクトリで npm install を実行します。
@:~/mean/angular-client$ npm install
> uws@0.14.5 install /home//mean/angular-client/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0
> node-sass@4.7.2 install /home//mean/angular-client/node_modules/node-sass
> node scripts/install.js
Downloading binary from https://github.com/sass/node-sass/releases/download/v4.7.2/linux-x64-57_binding.node
Download complete ] - :
Binary saved to /home//mean/angular-client/node_modules/node-sass/vendor/linux-x64-57/binding.node
Caching binary to /home//.npm/node-sass/4.7.2/linux-x64-57_binding.node
> uglifyjs-webpack-plugin@0.4.6 postinstall /home//mean/angular-client/node_modules/webpack/node_modules/uglifyjs-webpack-plugin
> node lib/post_install.js
> node-sass@4.7.2 postinstall /home//mean/angular-client/node_modules/node-sass
> node scripts/build.js
Binary found at /home//mean/angular-client/node_modules/node-sass/vendor/linux-x64-57/binding.node
Testing binary
Binary is fine
added 1457 packages from 1250 contributors in 80.009s
次に、express ディレクトリに移動し、npm install を実行します。
@:~/mean/angular-client$ cd ..
@:~/mean$ cd express-server/
@:~/mean/express-server$ npm install
すべての設定が完了したので、docker-compose.yml ファイルを実行します。これにより、すべての Docker コンテナーが起動され、MEAN スタック アプリケーションが実行されます。
@:~/mean/express-server$ cd ..
@:~/mean$ docker-compose up
Creating network "mean_default" with the default driver
Building angular
Step 1/8 : FROM node:8
8: Pulling from library/node
a4d8138d0f6b: Pull complete
dbdc36973392: Pull complete
f59d6d019dd5: Pull complete
aaef3e026258: Pull complete
6e454d3b6c28: Pull complete
c717a7c205aa: Pull complete
37add8e5ac11: Pull complete
0314ab675d31: Pull complete
012886364728: Pull complete
Digest: sha256:310db2abcff097ef44af205d81833282a6d5471002a1b59d7b7459a74152c856
Status: Downloaded newer image for node:8
---> 8e45c884a32e
Step 2/8 : RUN mkdir -p /var/www/app
---> Running in c70a0cab7994
Removing intermediate container c70a0cab7994
---> 001c5e840b24
Step 3/8 : WORKDIR /var/www/app
---> Running in 622ebdc41b2f
Removing intermediate container 622ebdc41b2f
---> baa2e2347259
Step 4/8 : COPY package.json /var/www/app
---> 5b97543befab
Step 5/8 : RUN npm install
---> Running in 73e3d8b7a701
> uws@9.14.0 install /var/www/app/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0
> node-sass@4.12.0 install /var/www/app/node_modules/node-sass
> node scripts/install.js
Downloading binary from https://github.com/sass/node-sass/releases/download/v4.12.0/linux-x64-57_binding.node
Download complete
Binary saved to /var/www/app/node_modules/node-sass/vendor/linux-x64-57/binding.node
Caching binary to /root/.npm/node-sass/4.12.0/linux-x64-57_binding.node
> core-js@2.6.9 postinstall /var/www/app/node_modules/core-js
> node scripts/postinstall || echo "ignore"
The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock
> uglifyjs-webpack-plugin@0.4.6 postinstall /var/www/app/node_modules/webpack/node_modules/uglifyjs-webpack-plugin
> node lib/post_install.js
> node-sass@4.12.0 postinstall /var/www/app/node_modules/node-sass
> node scripts/build.js
Binary found at /var/www/app/node_modules/node-sass/vendor/linux-x64-57/binding.node
Testing binary
Binary is fine
added 1606 packages from 1329 contributors and audited 15092 packages in 112.427s
Removing intermediate container 73e3d8b7a701
---> 55790d2fae93
Step 6/8 : COPY . /var/www/app
---> 61537aa487f4
Step 7/8 : EXPOSE 4200
---> Running in 632eedc35a45
Removing intermediate container 632eedc35a45
---> 51e75b0e2ebe
Step 8/8 : CMD ["npm", "start"]
---> Running in 36bbb12a0d38
Removing intermediate container 36bbb12a0d38
---> 9f8d61db600c
Successfully built 9f8d61db600c
Successfully tagged mean_angular:latest
Pulling database (mongo:)...
latest: Pulling from library/mongo
35b42117c431: Pull complete
ad9c569a8d98: Pull complete
293b44f45162: Pull complete
0c175077525d: Pull complete
4e73525b52ba: Pull complete
a22695a3f5e9: Pull complete
c5175bcf2977: Pull complete
3e320da07793: Pull complete
01c6db6b2b5a: Pull complete
3bd6e9d03e78: Pull complete
e03dcf51513f: Pull complete
c1956a9e136a: Pull complete
4c35cf22b1d5: Pull complete
Building express
Step 1/9 : FROM node:8
---> 8e45c884a32e
Step 2/9 : RUN mkdir -p /var/www/app
---> Using cache
---> 001c5e840b24
Step 3/9 : WORKDIR /var/www/app
---> Using cache
---> baa2e2347259
Step 4/9 : COPY package.json /var/www/app
---> 0232ad53c679
Step 5/9 : RUN npm install
---> Running in c309bf6f218e
added 128 packages from 151 contributors and audited 233 packages in 9.055s
Removing intermediate container c309bf6f218e
---> 49e652884562
Step 6/9 : RUN npm install -g nodemon
---> Running in 0ed5d7f3642b
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js
> nodemon@1.19.1 postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
+ nodemon@1.19.1
added 221 packages from 128 contributors in 18.856s
Removing intermediate container 0ed5d7f3642b
---> 32c55606f35e
Step 7/9 : COPY . /var/www/app
---> a618b38a2812
Step 8/9 : EXPOSE 3000
---> Running in bea389ab3ef1
Removing intermediate container bea389ab3ef1
---> 684bbfb31371
Step 9/9 : CMD ["npm", "start"]
---> Running in 9aa1b72e4a4e
Removing intermediate container 9aa1b72e4a4e
---> 35dcb3df9806
Successfully built 35dcb3df9806
Successfully tagged mean_express:latest
Creating mean_angular_1_de44b3f5b988 ... done
Creating mean_database_1_708f8f9c3c33 ... done
Creating mean_express_1_b57a483a72ee ... done
Attaching to mean_angular_1_f257e2233ef1, mean_database_1_ccc5c677e00b, mean_express_1_574f07b045fc
angular_1_f257e2233ef1 |
angular_1_f257e2233ef1 | > my-app@0.0.0 start /var/www/app
angular_1_f257e2233ef1 | > ng serve -H 0.0.0.0
angular_1_f257e2233ef1 |
database_1_ccc5c677e00b | 2019-07-20T22:33:25.933+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=f74b56905249
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] db version v4.0.10
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] allocator: tcmalloc
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] modules: none
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] build environment:
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] distmod: ubuntu1604
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] distarch: x86_64
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] target_arch: x86_64
database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL [initandlisten] options: { net: { bindIpAll: true } }
express_1_574f07b045fc |
express_1_574f07b045fc | > express-server@0.0.0 start /var/www/app
express_1_574f07b045fc | > nodemon ./bin/www
express_1_574f07b045fc |
express_1_574f07b045fc | [nodemon] 1.19.1
express_1_574f07b045fc | [nodemon] to restart at any time, enter `rs`
express_1_574f07b045fc | [nodemon] watching: *.*
express_1_574f07b045fc | [nodemon] starting `node ./bin/www`
database_1_ccc5c677e00b | 2019-07-20T22:33:33.543+0000 I NETWORK [listener] connection accepted from 172.19.0.4:38958 #1 (1 connection now open)
database_1_ccc5c677e00b | 2019-07-20T22:33:33.560+0000 I NETWORK [conn1] received client metadata from 172.19.0.4:38958 conn1: { driver: { name: "nodejs", version: "3.0.1" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.18.0-25-generic" }, platform: "Node.js v8.16.0, LE, mongodb-core: 3.0.1" }
express_1_574f07b045fc | mongodb: connected
angular_1_f257e2233ef1 | ** NG Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **
angular_1_f257e2233ef1 | Date: 2019-07-21T11:21:03.868Z - Hash: 639d9a968476ed482b5c - Time: 336ms
angular_1_f257e2233ef1 | 4 unchanged chunks
angular_1_f257e2233ef1 | chunk {main} main.bundle.js (main) 19.8 kB [initial] [rendered]
angular_1_f257e2233ef1 |
angular_1_f257e2233ef1 | webpack: Compiled successfully.
angular_1_f257e2233ef1 | webpack: Compiling...
angular_1_f257e2233ef1 | Date: 2019-07-21T11:25:15.661Z - Hash: e5a2b1c1afe0deb396c3 - Time: 251ms
angular_1_f257e2233ef1 | 4 unchanged chunks
angular_1_f257e2233ef1 | chunk {main} main.bundle.js (main) 19.8 kB [initial] [rendered]
angular_1_f257e2233ef1 |
angular_1_f257e2233ef1 | webpack: Compiled successfully.
ブラウザに移動して https://localhost:4200 を確認すると、アプリケーションが起動して実行されます。
https://localhost:3000 に移動して、Express サーバーが実行されているかどうかを確認します。
また、 docker image コマンドを実行して、docker に存在するすべてのイメージを確認することもできます。
@:~/mean$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mean_express latest 35dcb3df9806 14 hours ago 923MB
mean_angular latest 9f8d61db600c 14 hours ago 1.29GB
node 8 8e45c884a32e 9 days ago 895MB
mongo latest 785c65f61380 2 weeks ago 412MB
以下のコマンドを実行して、docker 内で実行されているコンテナーを確認します。
@:~/mean$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
681c9c34bee2 mean_express "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:3000->3000/tcp mean_express_1_574f07b045fc
f74b56905249 mongo "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:27017->27017/tcp mean_database_1_ccc5c677e00b
260ef1e52dab mean_angular "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:4200->4200/tcp mean_angular_1_f257e2233ef1
これで、3 つの Docker コンテナーがすべて稼働状態になりました。
Docker-compose は、すべてのコンテナーの実行を簡単に処理します。これは 3 つのコンテナを実行する簡単な例でした。アプリケーションを数百のコンテナ上で起動する必要がある場合、これがどれほど便利になるか想像できるでしょう。さあ、それがどのように機能するかを試してみてください。