docker compose run

読む時間の目安: 4 分

説明

サービスに対するワンタッチ(one-off)のコマンドを実行します。

利用方法

$ docker compose run [オプション] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]

追加説明

Runs a one-time command against a service.

the following command starts the web service and runs bash as its command:

$ docker compose run web bash

Commands you use with run start in new containers with configuration defined by that of the service, including volumes, links, and other details. However, there are two important differences:

First, the command passed by run overrides the command defined in the service configuration. For example, if the web service configuration is started with bash, then docker compose run web python app.py overrides it with python app.py.

The second difference is that the docker compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports

$ docker compose run --service-ports web python manage.py shell

Alternatively, manual port mapping can be specified with the --publish or -p options, just as when using docker run:

$ docker compose run --publish 8080:80 -p 2022:22 -p 127.0.0.1:2021:21 web python manage.py shell

If you start a service configured with links, the run command first checks to see if the linked service is running and starts the service if it is stopped. Once all the linked services are running, the run executes the command you passed it. For example, you could run:

$ docker compose run db psql -h db -U docker

This opens an interactive PostgreSQL shell for the linked db container.

If you do not want the run command to start linked containers, use the --no-deps flag:

$ docker compose run --no-deps web python manage.py shell

If you want to remove the container after running while overriding the container’s restart policy, use the --rm flag:

$ docker compose run --rm web python manage.py db upgrade

This runs a database upgrade script, and removes the container when finished running, even if a restart policy is specified in the service configuration.

オプション

名前/省略形 デフォルト 説明
--detach , -d コンテナーをバックグラウンドで実行し、コンテナー ID を表示します。
--entrypoint イメージのエントリーポイントをオーバーライドします。
--env , -e 環境変数を設定します。
--labels , -l ラベルを追加またはオーバーライドします。
--name コンテナーに対して名前を割り当てます。
--no-TTY , -T 擬似 noTTY への割り当てを無効にします。デフォルトにおいて docker-compose run には TTY が割り当てられます。
--no-deps リンクされているサービスは起動しません。
--publish , -p ホストに対してコンテナーのポートを公開します。
--rm コンテナーの終了時にはコンテナーを自動削除します。
--service-ports サービスポートを有効にしホストへのマップを行ってコマンドを実行します。
--use-aliases コンテナーが接続するネットワークにおいて、サービスネットワークの useAliases を利用します。
--user , -u 指定されたユーザーまたは UID によりコマンドを実行します。
--volume , -v ボリュームをバインドマウントします。
--workdir , -w コンテナー内のワーキングディレクトリを指定します。

上位コマンド

コマンド 説明
docker compose Docker Compose コマンド
コマンド 説明
docker compose build サービスのビルドまたは再ビルド
docker compose convert Compose ファイルをプラットフォームの標準的な書式に変換します。
docker compose cp Copy files/folders between a service container and the local filesystem
docker compose create サービスコンテナーを生成します。
docker compose down コンテナーとネットワークを停止して削除します。
docker compose events コンテナーからのリアルタイムイベントを受信します。
docker compose exec 実行中コンテナー内においてコマンドを実行します。
docker compose images 生成されたコンテナーにおいて利用されているイメージを一覧表示します。
docker compose kill サービスコンテナーを強制的に停止します。
docker compose logs View output from containers
docker compose ls 実行中の Compose プロジェクトを一覧表示します。
docker compose pause サービスを一時停止します。
docker compose port Print the public port for a port binding.
docker compose ps コンテナーを一覧表示します。
docker compose pull サービスイメージをプルします。
docker compose push サービスイメージをプッシュします。
docker compose restart Restart containers
docker compose rm 停止しているサービスコンテナーを削除します。
docker compose run サービスに対するワンタッチ(one-off)のコマンドを実行します。
docker compose start サービスを起動します。
docker compose stop サービスを停止します。
docker compose top 実行中プロセスを表示します。
docker compose unpause 停止中サービスを再開します。
docker compose up コンテナーを生成して起動します。