docker-compose COMMAND
の形式でdocker-compose.yml
を読み込み、複数コンテナを管理することができます。ここでは、docker-composeコマンドの主な利用方法を解説します。
目次
docker-composeの使い方
docker-compose
と入力すると利用できる操作一覧を確認できます。
$ 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
主な操作について紹介します。
up|コンテナ作成 & コマンド起動
サービスを指定して実行
$ docker-compose up mysql
Creating network "wordpress_default" with the default driver
Creating wordpress_mysql_1 ... done
Attaching to wordpress_mysql_1
mysql_1 | 2018-09-16T11:09:49.354918Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
(省略)
^CGracefully stopping... (press Ctrl+C again to force)
Stopping wordpress_mysql_1 ... done
$
全サービス実行
$ docker-compose up
Creating network "wordpress_default" with the default driver
Creating wordpress_mysql_1 ... done
Creating wordpress_wordpress_1 ... done
Attaching to wordpress_mysql_1, wordpress_wordpress_1
wordpress_1 | WordPress not found in /var/www/html - copying now...
mysql_1 | 2018-09-16T11:08:15.996513Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
(省略)
^CGracefully stopping... (press Ctrl+C again to force)
Killing wordpress_wordpress_1 ... done
Killing wordpress_mysql_1 ... done
$
バッググラウンドで実行
( -dオプション
)
-dオプション
を利用すると、バッググラウンドで実行してくれます。
$ docker-compose up -d
Creating network "wordpress_default" with the default driver
Creating wordpress_mysql_1 ... done
Creating wordpress_wordpress_1 ... done
$
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
wordpress_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
wordpress_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:10080->80/tcp
イメージの再構築
( --buildオプション
)
--buildオプション
を利用すると、イメージの再構築を同時に行ってくれます。
$ docker-compose up -d --build
exec|既存コンテナでコマンド実行
既に立ち上がっているコンテナでコマンド実行します。
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a6b6534e216 wordpress:latest "docker-entrypoint.s…" 10 seconds ago Up 9 seconds 0.0.0.0:10080->80/tcp wordpress_wordpress_1
8659ea5e7a65 mysql:5.7 "docker-entrypoint.s…" 11 seconds ago Up 10 seconds 3306/tcp, 33060/tcp wordpress_mysql_1
$
$ docker-compose exec mysql echo 'hello'
hello
$
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a6b6534e216 wordpress:latest "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 0.0.0.0:10080->80/tcp wordpress_wordpress_1
8659ea5e7a65 mysql:5.7 "docker-entrypoint.s…" 20 seconds ago Up 19 seconds 3306/tcp, 33060/tcp wordpress_mysql_1
run|コンテナ作成 & コマンド実行
新しくコンテナを作成してからコマンド実行します。
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a6b6534e216 wordpress:latest "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 0.0.0.0:10080->80/tcp wordpress_wordpress_1
8659ea5e7a65 mysql:5.7 "docker-entrypoint.s…" 20 seconds ago Up 19 seconds 3306/tcp, 33060/tcp wordpress_mysql_1
$
$ docker-compose run mysql echo 'hello'
hello
$
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7af979355acc mysql:5.7 "docker-entrypoint.s…" 3 seconds ago Exited (0) 2 seconds ago wordpress_mysql_run_1
5a6b6534e216 wordpress:latest "docker-entrypoint.s…" 30 seconds ago Up 29 seconds 0.0.0.0:10080->80/tcp wordpress_wordpress_1
8659ea5e7a65 mysql:5.7 "docker-entrypoint.s…" 31 seconds ago Up 30 seconds 3306/tcp, 33060/tcp wordpress_mysql_1
stop|コンテナ停止
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8882a7dfd653 wordpress:latest "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:10080->80/tcp wordpress_wordpress_1
0d9a20e439d0 mysql:5.7 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 3306/tcp, 33060/tcp wordpress_mysql_1
$
$ docker-compose stop
Stopping wordpress_wordpress_1 ... done
Stopping wordpress_mysql_1 ... done
$
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8882a7dfd653 wordpress:latest "docker-entrypoint.s…" 36 seconds ago Exited (0) 15 seconds ago wordpress_wordpress_1
0d9a20e439d0 mysql:5.7 "docker-entrypoint.s…" 37 seconds ago Exited (0) 13 seconds ago wordpress_mysql_1
rm|停止コンテナを削除
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8882a7dfd653 wordpress:latest "docker-entrypoint.s…" 36 seconds ago Exited (0) 15 seconds ago wordpress_wordpress_1
0d9a20e439d0 mysql:5.7 "docker-entrypoint.s…" 37 seconds ago Exited (0) 13 seconds ago wordpress_mysql_1
$
$ docker-compose rm
Going to remove wordpress_wordpress_1, wordpress_mysql_1
Are you sure? [yN] y
Removing wordpress_wordpress_1 ... done
Removing wordpress_mysql_1 ... done
$
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$
down|コンテナ停止 & 削除
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2d87ea4b199 wordpress:latest "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:10080->80/tcp wordpress_wordpress_1
1ce9113aba36 mysql:5.7 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 3306/tcp, 33060/tcp wordpress_mysql_1
$
$ docker-compose down
Stopping wordpress_wordpress_1 ... done
Stopping wordpress_mysql_1 ... done
Removing wordpress_wordpress_1 ... done
Removing wordpress_mysql_1 ... done
Removing network wordpress_default
$
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$
--rmi all
をつけるとイメージも削除します。-v
をつけるとVolumeも削除します。
ps|コンテナ表示
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
wordpress_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
wordpress_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:10080->80/tcp
logs|ログ表示
全サービスのログ表示
$ docker-compose logs
指定サービスのログ表示
$ docker-compose logs mysql
images|Dockerイメージ表示
$ docker-compose images
Container Repository Tag Image Id Size
-------------------------------------------------------------------
wordpress_mysql_1 mysql 5.7 563a026a1511 355 MB
wordpress_wordpress_1 wordpress latest ca0fefec932b 390 MB
top|起動中プロセス表示
$ docker-compose top
wordpress_mysql_1
PID USER TIME COMMAND
-----------------------------
26901 999 0:00 mysqld
wordpress_wordpress_1
PID USER TIME COMMAND
------------------------------------------
27092 root 0:00 apache2 -DFOREGROUND
27459 xfs 0:00 apache2 -DFOREGROUND
27460 xfs 0:00 apache2 -DFOREGROUND
27461 xfs 0:00 apache2 -DFOREGROUND
27462 xfs 0:00 apache2 -DFOREGROUND
27463 xfs 0:00 apache2 -DFOREGROUND