Docker-compose run command detailed and tested

docker-compose is the official docker container orchestration tool, responsible for rapid distributed deployment of applications

Command object and format
For Compose, the object of most commands can be either the project itself or the service or container in the project. If there is no special description, the command object will be the project, which means that all services in the project will be affected by the command.

Execute docker-compose [COMMAND] --help or docker-compose help [COMMAND] to view the usage format of a specific command.

The basic usage format of the docker-compose command is

docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]

Command options

-f, --file FILE 指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定。

-p, --project-name NAME 指定项目名称,默认将使用所在目录名称作为项目名。

--x-networking 使用 Docker 的可拔插网络后端特性

--x-network-driver DRIVER 指定网络后端的驱动,默认为 bridge

--verbose 输出更多调试信息。

-v, --version 打印版本并退出。

Command instructions The
build
format is

docker-compose build [options] [SERVICE...]

Build (rebuild) the service container in the project.

Once the service container is built, it will carry a tag name. For example, for a db container in a web project, it may be web_db.

You can run docker-compose build in the project directory at any time to rebuild the service.

选项包括:

--force-rm 删除构建过程中的临时容器。

--no-cache 构建镜像过程中不使用 cache(这将加长构建过程)。

--pull 始终尝试通过 pull 来获取更新版本的镜像。

config
verifies whether the Compose file format is correct, if it is correct, it displays the configuration, if the format is wrong, it displays the reason for the error.

The down
command will stop the container started by the up command and remove the network

exec
enters the specified container.

help
Get help for a command.

images
lists the images contained in the Compose file.

The kill
format is

docker-compose kill [options] [SERVICE...]

By sending the SIGKILL signal to forcibly stop the service container.

Support the -s parameter to specify the signal to be sent, for example, send the SIGINT signal through the following command.

$ docker-compose kill -s SIGINT

The logs
format is

 docker-compose logs [options] [SERVICE...]

View the output of the service container. By default, docker-compose will use different colors to distinguish different service outputs. The color can be turned off by --no-color.

This command is very useful when debugging problems.

The pause
format is

 docker-compose pause [SERVICE...]

Pause a service container.

The port
format is

docker-compose port [options] SERVICE PRIVATE_PORT

Print the public port mapped by a container port.

选项:

--protocol=proto 指定端口协议,tcp(默认值)或者 udp。

--index=index 如果同一服务存在多个容器,指定命令对象容器的序号(默认为 1)。

ps
format is

docker-compose ps [options] [SERVICE...]

List all current containers in the project.

选项:

-q 只打印容器的 ID 信息。

The pull
format is

docker-compose pull [options] [SERVICE...]

Pull the image that the service depends on.

选项:

--ignore-pull-failures 忽略拉取镜像过程中的错误。

push
push service to Docker dependent mirror mirror warehouse.

The
format of restart is

docker-compose restart [options] [SERVICE...]

Restart the services in the project.

Options:

-t, --timeout TIMEOUT Specifies the timeout for stopping the container before restarting (10 seconds by default).

rm
format is

docker-compose rm [options] [SERVICE...]

Delete all (stopped) service containers. It is recommended to execute the docker-compose stop command first to stop the container.

选项:

-f, --force 强制直接删除,包括非停止状态的容器。一般尽量不要使用该选项。

-v 删除容器所挂载的数据卷。

The run
format is

docker-compose run [options] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

Execute a command on the specified service.

E.g:

$ docker-compose run ubuntu ping docker.com

A ubuntu service container will be started and the ping docker.com command will be executed.

By default, if there is an association, all associated services will be automatically started unless these services are already running.

This command is similar to running the specified command after starting the container, and related volumes, links, etc. will be automatically created according to the configuration.

Two differences:

The given command will overwrite the original auto-run command;

The port is not automatically created to avoid conflicts.

If you do not want to automatically start the associated container, you can use the --no-deps option, for example

$ docker-compose run --no-deps web python manage.py shell
will not start other containers associated with the web container.

选项:

-d 后台运行容器。

--name NAME 为容器指定一个名字。

--entrypoint CMD 覆盖默认的容器启动指令。

-e KEY=VAL 设置环境变量值,可多次使用选项来设置多个环境变量。

-u, --user="" 指定运行容器的用户名或者 uid。

--no-deps 不自动启动关联的服务容器。

--rm 运行命令后自动删除容器,d 模式下将忽略。

-p, --publish=[] 映射容器端口到本地主机。

--service-ports 配置服务端口并映射到本地主机。

-T 不分配伪 tty,意味着依赖 tty 的指令将无法运行。

The scale
format is

 docker-compose scale [options] [SERVICE=NUM...]

Set the number of containers running the specified service.

Set the number by the parameter of service=num. E.g:

$ docker-compose scale web=3 db=2

Will start 3 containers to run web services, and 2 containers to run db services.

Generally, when the specified number is more than the actual running container of the service, a new container will be created and started; otherwise, the container will be stopped.

选项:

-t, --timeout TIMEOUT 停止容器时候的超时(默认为 10 秒)。

start
format is

docker-compose start [SERVICE...]

Start the existing service container.

The stop
format is

docker-compose stop [options] [SERVICE...]

Stop the container that is already running, but do not delete it. These containers can be started again through docker-compose start.

选项:

-t, --timeout TIMEOUT 停止容器时候的超时(默认为 10 秒)。

top
view the processes running in each service container.

The unpause
format is

docker-compose unpause [SERVICE...]

Resume services that are in a suspended state.

up
format is

docker-compose up [options] [SERVICE...]

This command is very powerful, it will try to automatically complete a series of operations including building an image, (re)creating a service, starting the service, and associating service-related containers.

All linked services will be automatically started unless they are already running.

It can be said that most of the time, a project can be started directly through this command.

By default, all containers started by docker-compose up are in the foreground, and the console will print the output information of all containers at the same time, which can be very convenient for debugging.

When the command is stopped by Ctrl-C, all containers will be stopped.

If you use docker-compose up -d, all containers will be started and run in the background. It is generally recommended to use this option in a production environment.

By default, if the service container already exists, docker-compose up will try to stop the container and then recreate it (keep the volume mounted from volumes-from) to ensure that the newly started service matches the latest content of the docker-compose.yml file . If the user does not want the container to be stopped and recreated, docker-compose up --no-recreate can be used. This will only start containers that are in a stopped state and ignore services that are already running. If users only want to redeploy a certain service, they can use docker-compose up --no-deps -d <SERVICE_NAME> to recreate the service and stop the old service in the background, start the new service, and it will not affect the services it depends on .

选项:

-d 在后台运行服务容器。

--no-color 不使用颜色来区分不同的服务的控制台输出。

--no-deps 不启动服务所链接的容器。

--force-recreate 强制重新创建容器,不能与 --no-recreate 同时使用。

--no-recreate 如果容器已经存在了,则不重新创建,不能与 --force-recreate 同时使用。

--no-build 不自动构建缺失的服务镜像。

-t, --timeout TIMEOUT 停止容器时候的超时(默认为 10 秒)。

The version
format is

docker-compose version

Print version information.

Help document: https://yeasy.gitbooks.io/docker_practice/compose/commands.html

Guess you like

Origin blog.csdn.net/ZhanBiaoChina/article/details/105453609