Compose
13.1 Compose命令说明
13.1.1 Compose命令格式
对于Compose来说,大部分命令的对象既可以是项目本身,也可以指定为项目中的服务或者容器。如果没有特别的说明,命令对象将是项目,这意味着项目中所有的服务都会受到命令影响。
执行docker-compose [COMMAND] --help
或者docker-compose help [COMMAND]
可以查看具体某个命令的使用格式。Compose命令的基本的使用格式是:
docker-compose [OPTIONS] COMMAND
OPTIONS命令选项如下:
- -f, --file FILE: 指定使用的Compose模板文件,默认为docker-compose.yml,可以多次指定;
- -p, --project-name NAME: 指定项目名称,默认将使用所在目录名称作为项目名;
- –verbose: 输出更多调试信息;
- -v, --version: 打印版本并退出;
- -H, --host HOST:指定所操作的Docker服务地址;
- -tls: 启用TLS,如果指定-tlsverify则默认开启;
- -tlscacert CA_PATH:信任的TLS CA的证书;
- -tlscert CLIENT_CERT_PATH:客户端使用的TLS证书;
- -tlskey TLS_KEY_PATH: TLS的私钥文件路径;
- -tlsverify:使用TLS校验连接对方;
- -skip-hostname-check: 不使用TLS证书校验对方的主机名;
- -project-directory PATH: 指定工作目录,默认为Compose文件所在路径。
13.1.2 Compose命令列表
命令 | 功能 |
---|---|
build | 构建(重新构建)项目中的服务容器 |
config | 校验和查看Compose文件的配置信息 |
down | 停止服务栈,并删除相关资源,包括容器、挂载卷、网络、创建镜像等默认情况下只清除所创建的容器和网络资源 |
events | 实时监控容器的事件信息 |
exec | 在一个运行中的容器内执行给定命令 |
help | 获得一个命令的帮助 |
images | 列出服务所创建的镜像 |
kill | 通过发送SIGKILL信号来强制停止服务容器 |
logs | 查看服务容器的输出 |
pause | 暂停一个服务容器 |
port | 打印某个容器端口所映射的公共端口 |
ps | 列出项目中目前的所有容器 |
pull | 拉取服务依赖的镜像 |
push | 推送服务创建的镜像到镜像仓库 |
restart | 重启项目中的服务 |
rm | 删除所有(停止状态的)服务容器 |
run | 在指定服务上执行一个命令 |
scale | 设嚣指定服务运行的容器个数 |
start | 启动已经存在的服务容器 |
stop | 停止已经处于运行状态的容器,但不删除它 |
top | 显示服务栈中正在运行的进程信息 |
unpause | 恢复处于暂停状态中的服务 |
up | 尝试自动完成一系列操作·包括构建镜像,(重新)创建服务,启动服务,并关联服务相关容器等 |
version | 打印版本信息 |
13.1.3 命令详细说明
1.build
构建(重新构建)项目中的服务容器。格式为:
Usage: docker compose build [OPTIONS] [SERVICE...]
Build or rebuild services
Options:
--build-arg key=val 指定服务创建时的参数。
--no-cache 构建镜像过程中不使用cache(这将加长构建过程);
--progress string Set type of progress output (auto, tty, plain, quiet) (default "auto")
--pull 始终尝试通过pull来获取更新版本的镜像;
-q, --quiet Don't print anything to STDOUT
--ssh string Set SSH authentications used when building service images. (use 'default' for using your default
SSH Agent)
服务容器一旦构建后,将会带上一个标记名,例如对于Web项目中的一个db容器, 可能是web_db。
可以随时在项目目录下运行docker-compose build
来重新构建服务。
[root@dbc-server-554 compose]# docker-compose -f compose.yml build --no-cache
[+] Building 0.6s (5/5) FINISHED
=> [internal] load build definition from dockerfile 0.2s
=> => transferring dockerfile: 31B 0.0s
=> [internal] load .dockerignore 0.2s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/training/webapp:latest 0.0s
=> CACHED [1/1] FROM docker.io/training/webapp 0.0s
=> exporting to image 0.3s
=> => exporting layers 0.0s
=> => writing image sha256:da8435f95735178f0435a1cb3fa6cbff47f2a6f819a90bcc134b6de7d74ff491 0.0s
=> => naming to docker.io/library/compose-app 0.0s
[root@dbc-server-554 compose]# docker-compose -f compose.yml build -q
[root@dbc-server-554 compose]#
2. config
格式为docker-compose config [options]
。校验和查看Compose文件的配置信息。支持选项包括:
- –resolve-image-digests:为镜像添加对应的摘要信息;
- -q, --quite: 只检验格式正确与否,不输出内容;
- –services: 打印出Compose中所有的服务信息;
- –volumes: 打印出Compose中所有的挂载卷信息;
[root@dbc-server-554 compose]# docker-compose -f compose.yml config
name: compose
services:
app:
build:
context: /root/docker/compose
dockerfile: Dockerfile
networks:
default: null
ports:
- mode: ingress
target: 5000
published: "5000"
protocol: tcp
networks:
default:
name: compose_default
3.down
格式为docker-compose down [options]
。停止服务栈,并删除相关资源,包括容器、 挂载卷、 网络、 创建镜像等。默认情况下只清除所创建的容器和网络资源。支持选项包括:
- -rmi type: 指定删除镜像的类型,包括all(所有镜像),local(仅本地);
- -v, --volumes: 删除挂载数据卷;
- -remove-orphans: 清除孤儿容器,即未在Compose服务中定义的容器;
- -t,–timeout TIMEOUT:指定超时时间,默认为10s。
[root@dbc-server-554 compose]# docker-compose -f compose.yml down
[+] Running 1/1
⠿ Network compose_default Removed
4.events
格式为docker-composeevents [options] [SERVICE...]
。实时监控容器的事件信息。支持选项包括
–json:以Json对象流格式输出事件信息。
5.exec
在一个运行中的容器内执行给定命令。
Usage: docker compose exec [OPTIONS] SERVICE COMMAND [ARGS...]
Execute a command in a running container.
Options:
-d, --detach Detached mode: Run command in the background.
-e, --env key=value Set environment variables
--index int index of the container if there are multiple instances of a service [default: 1]. (default 1)
-T, --no-TTY docker compose exec Disable pseudo-TTY allocation. By default docker compose exec allocates a TTY.
--privileged Give extended privileges to the process.以特权角色运行命令;
-u, --user string Run the command as this user.
-w, --workdir string Path to workdir directory for this command.
[root@dbc-server-554 compose]# docker-compose -f compose.yml exec -w /root/ -u root -e a=1 app env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=d1b39b09ec1e
TERM=xterm
a=1
HOME=/root
6.images
格式为docker-compose images [options] [SERVICE... ]
。列出服务所创建的镜像。支持选项为:
- -q: 仅显示镜像的ID。
[root@dbc-server-554 compose]# docker-compose -f compose.yml images
CONTAINER REPOSITORY TAG IMAGE ID SIZE
compose-app-1 compose-app latest da8435f95735 349MB
[root@dbc-server-554 compose]# docker-compose -f compose.yml images -q
da8435f95735178f0435a1cb3fa6cbff47f2a6f819a90bcc134b6de7d74ff491
7.kill
格式为docker-compose kill [options] [SERVICE ...]
。通过发送SIGKILL信号来强制停止服务容器。
支持通过-s
参数来指定发送的信号,例如通过如下指令发送SIGINT信号。
[root@dbc-server-554 compose]# docker-compose -f compose.yml kill -s SIGINT
[+] Running 1/0
⠿ Container compose-app-1 Killed
8.logs
查看服务容器的输出。默认情况下,docker-compose将对不同的服务输出使用不同的颜色来区分。可以通过--no-color
来关闭颜色。该命令在调试问题的时候十分有用。
Usage: docker compose logs [OPTIONS] [SERVICE...]
View output from containers
Options:
-f, --follow Follow log output.
--no-color Produce monochrome output.
--no-log-prefix Don't print prefix in logs.
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
--tail string Number of lines to show from the end of the logs for each container. (default "all")
-t, --timestamps Show timestamps.
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
[root@dbc-server-554 compose]# docker-compose -f compose.yml logs --no-color
compose-app-1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
compose-app-1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[root@dbc-server-554 compose]# docker-compose -f compose.yml logs -f
compose-app-1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
compose-app-1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
^Ccanceled
[root@dbc-server-554 compose]# docker-compose -f compose.yml logs -t
compose-app-1 | 2023-01-31T02:37:27.511108004Z * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
compose-app-1 | 2023-01-31T03:05:41.454952535Z * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[root@dbc-server-554 compose]# docker-compose -f compose.yml logs --tail 1
compose-app-1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
9.pause/unpause
暂停/恢复处于暂停的一个服务容器。
格式为docker-compose [un]pause [SERVICE... ]
10.port
打印某个容器端口所映射的公共端口。
Usage: docker compose port [OPTIONS] SERVICE PRIVATE_PORT
Print the public port for a port binding.
Options:
--index int index of the container if service has multiple replicas (default 1)
--protocol string tcp or udp (default "tcp")
[root@dbc-server-554 compose]# docker-compose -f compose.yml port --protocol=tcp app 5000
0.0.0.0:5000
[root@dbc-server-554 compose]# docker-compose -f compose.yml port --protocol=tcp app 4000
no port 4000/tcp for container compose-app-1: 5000/tcp, 5000/tcp
11. ps
列出项目中目前的所有容器
格式为docker-compose ps [OPTIONS] SERVICE
。选项包括
-q
:只打印容器的ID信息。
[root@dbc-server-554 compose]# docker-compose -f compose.yml ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
compose-app-1 compose-app "/bin/sh -c 'python …" app 38 minutes ago Up 9 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp
[root@dbc-server-554 compose]# docker-compose -f compose.yml ps -q
d1b39b09ec1e2e588cd5b51e0c14f629fbae7d9c2d3be09d3207917b19bf48c2
12.push/pull
推送服务创建的镜像到镜像仓库 / 拉取服务依赖的镜像。
Usage: docker compose push/pull [OPTIONS] [SERVICE...]
Push service images
Options:
--ignore-push-failures Push what it can and ignores images with push failures#忽略推送镜像过程中的错误
--include-deps Also push images of services declared as dependencies
-q, --quiet Push without printing progress information
13.restart
Usage: docker compose restart [OPTIONS] [SERVICE...]
Restart service containers
Options:
-t, --timeout int Specify a shutdown timeout in seconds (default 10)
14.rm
删除所有(停止状态的)服务容器。推荐使用参数-s
来停止容器
Usage: docker compose rm [OPTIONS] [SERVICE...]
Removes stopped service containers
By default, anonymous volumes attached to containers will not be removed. You
can override this with -v. To list all volumes, use "docker volume ls".
Any data which is not in a volume will be lost.
Options:
-f, --force Don't ask to confirm removal
-s, --stop Stop the containers, if required, before removing
-v, --volumes Remove any anonymous volumes attached to containers
[root@dbc-server-554 compose]# docker-compose -f compose.yml rm -s app
[+] Running 1/1
⠿ Container compose-app-1 Stopped 0.4s
? Going to remove compose-app-1 Yes
[+] Running 1/0
⠿ Container compose-app-1 Removed 0.0s
15.run
在指定服务上执行一个命令,格式:
Usage: docker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...]
Run a one-off command on a service.
Options:
--build Build image before starting container.
-d, --detach Run container in background and print container ID
--entrypoint CMD Override the entrypoint of the image#覆盖默认的容器启动指令
-e, --env stringArray Set environment variables
-i, --interactive Keep STDIN open even if not attached. (default true)
-l, --label stringArray Add or override a label
--name string Assign a name to the container
-T, --no-TTY Disable pseudo-TTY allocation (default: auto-detected).#不分配伪tty,意味着依赖tty的指令将无法运行。
--no-deps Don't start linked services.#不自动启动关联的服务容器
-p, --publish stringArray Publish a container's port(s) to the host.#映射容器端口到本地主机
--quiet-pull Pull without printing progress information.
--rm Automatically remove the container when it exits#运行命令后自动删除容器,d模式下将忽略
--service-ports Run command with the service's ports enabled and mapped to the host.
--use-aliases Use the service's network useAliases in the network(s) the container connects to.
-u, --user string Run as specified username or uid
-v, --volume stringArray Bind mount a volume.
-w, --workdir string Working directory inside the container
[root@dbc-server-554 compose]# docker-compose run app ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.050 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.062 ms
将会启动一个app服务容器,并执行ping localhost
命令。
默认情况下,如果存在关联,则所有关联的服务将会自动被启动,除非这些服务已经在运行中。如果不希望自动启动关联的容器,可以使用--no-deps
选项
该命令类似启动容器后运行指定的命令,相关卷、链接等等都将会按照配置自动创建。两个不同点:
- 给定命令将会覆盖原有的自动运行命令;
- 会自动创建端口,以避免冲突。
16.scale
格式为docker-compose scale [options][ SERVICE=NUM...]
。
设置指定服务运行的容器个数。通过service=num的参数来设置数量。例如:
$ docker-compose scale web=3 db=2
将启动3个容器运行web服务,2个容器运行db服务。
一般的,当指定数目多于该服务当前实际运行容器,将新创建并启动容器;反之,将停止容器。选项包括
- -t,–timeout TIMEOUT:停止容器时候的超时(默认为10秒)。
17.start
启动已
经存在的服务容器。格式为
docker-compose start [SERVICE...]
18.stop
停止已经处于运行状态的容器,但不删除它。格式为
docker-compose stop [options] [SERVICE ... ]
通过docker-compose start
可以再次启动这些容器。选项包括
- -t,–timeout TIMEOUT:停止容器时候的超时(默认为10秒)。
19.top
显示服务栈中正在运行的进程信息。格式为
docker-compose top [SERVICE ... ]
20.*up
该命令十分强大,它将尝试自动完成包括构建镜像,(重新)创建服务,启动服务,并关联服务相关容器的一系列操作。链接的服务都将会被自动启动,除非已经处于运行状态。可以说,大部分时候都可以直接通过该命令来启动一个项目。
默认情况,docker-compose up
启动的容器都在前台,控制台将会同时打印所有容器的输出信息,可以很方便进行调试。当通过Ctrl-C停止命令时,所有容器将会停止。
如果使用docker-compose up -d
, 将会在后台启动并运行所有的容器。一般推荐生产环境下使用该选项。
默认情况,如果服务容器已经存在,docker-compose up
将会尝试停止容器,然后重新创建(保待使用volumes-from挂载的卷),以保证新启动的服务匹配docker-compose.yml文件的最新内容。
如果用户不希望容器被停止并重新创建,可以使用docker-compose up --no-recreate
。这样将只会启动处于停止状态的容器,而忽略已经运行的服务。
如果用户只想重新部署某个服务,可以使用docker-compose up --no-deps -d <SERVICE_NAME>
来重新创建服务并后台停止旧服务,启动新服务,并不会影响到其所依赖的服务。
命令格式
Usage: docker compose up [OPTIONS] [SERVICE...]
Create and start containers
Options:
--abort-on-container-exit Stops all containers if any container was stopped. Incompatible with -d# 当有容器停止时中止整个服务,与-d选项冲突。
--always-recreate-deps Recreate dependent containers. Incompatible with --no-recreate.
--attach stringArray Attach to service output.
--attach-dependencies Attach to dependent containers.
--build Build images before starting containers.
-d, --detach Detached mode: Run containers in the background
--exit-code-from string Return the exit code of the selected service container. Implies --abort-on-container-exit#退出时返回指定服务容器的退出符;
--force-recreate Recreate containers even if their configuration and image haven't changed.#强制重新创建容器,不能与--no-recreate同时使用;
--no-attach stringArray Don't attach to specified service.
--no-build Don't build an image, even if it's missing.#不自动构建缺失的服务镜像;
--no-color Produce monochrome output.#不使用颜色来区分不同的服务的控制台输出
--no-deps Don't start linked services.#不启动服务所链接的容器;
--no-log-prefix Don't print prefix in logs.
--no-recreate If containers already exist, don't recreate them. Incompatible with --force-recreate.#如果容器已经存在了,则不重新创建,不能与--force-recreate同时使用;
--no-start Don't start the services after creating them.
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--quiet-pull Pull without printing progress information.
--remove-orphans Remove containers for services not defined in the Compose file.#删除服务中未定义的孤儿容器;
-V, --renew-anon-volumes Recreate anonymous volumes instead of retrieving data from the previous containers.
--scale scale Scale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present.#扩展指定服务实例到指定数目。
-t, --timeout int Use this timeout in seconds for container shutdown when attached or when containers are already running. (default 10)#停止容器时候的超时(默认为10秒), 与-d选项冲突;
--timestamps Show timestamps.
--wait Wait for services to be running|healthy. Implies detached mode.
21.version
打印版本信息。格式为
docker-compose version
13.2 Compose环境变量
变量 | 说明 |
---|---|
COMPOSE_PROJECT_NAME | 设置Compose的项目名称,默认是当前工作目录(docker-compose.yml 文件所在目录)的名字Compose会为每一个启动的容器前添加的项目名称。例如, 一个名称为proj的项目, 其中的web容器名称可能为proj_web_l |
COMPOSE_FILE | 设置要使用的docker-compose.yml的路径。如果不指定,默认会先查找当前工作目录下是否存在docker-compose.yml文件, 如果还找不到,则继续查找上层目录 |
COMPOSE_API_VERSION | 某些情况下,Compose发出的Docker请求,其版本可能在服务端并不支持, 可以通过指定API版本来临时解决这个问题在生产环境中不推荐使用这个临时方案,要通过适当的升级来保证客户端和服务端版本的兼容性 |
DOCKER_HOST | 设置Docker服务端的监听地址。默认使用unix:///var/run/docker.sock, 这其实也是Docker客户端采用的默认值 |
DOCKER_TLS_VERIFY | 如果该环境变址不为空,则与Docker服务端的所有交互都通过TLS协议进行加密 |
DOCKER_CERT_PATH | 配置TLS通信所需要的验证文件(包括ca.pem、cert.pem和key.pem)的路径,默认是-/.docker |
COMPOSE_HTTP_TIMEOUT | Compose向Docker服务端发送请求的超时,默认值为60s |
COMPOSE_TLS_VERSION | 指定与Docker服务进行交互的TLS版本, 支持版本为TLSv!(默认值)、TLSvl_l、TLSvl_2 |
COMPOSE_PATH_SEPARATOR | 指定COMPOSE_FILE环境变噩中的路径间隔符 |
COMPOSE_IGNORE_ORPHANS | 是否忽略孤儿容器 |
COMPOSE_PARALLEL_LIMIT | 设置Compose可以执行进程的并发数 |
COMPOSE_INTERACTIVE_NO_CLI | 尝试不使用Docker命令行来执行run和exec指令 |
以DOCKER_开头的变量和用来配置Docker命令行客户端的使用一样。