Docker-compose命令-docker-compose

命令

docker-compose

描述

Define and run multi-container applications with Docker
使用Docker定义和运行多个容器应用

用法

1
docker-compose [-f <arg>...] [--profile <name>...] [options] [--] [COMMAND] [ARGS...]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Options:
-f, --file FILE 指定compose配置文件,默认为指定目录下的docker-compose.yml文件 (default: docker-compose.yml)
-p, --project-name NAME 指定项目名称,默认为目录名 (default: directory name)
--profile NAME 指定要启用的配置文件
-c, --context NAME 指定上下文名称
--verbose 显示详情
--log-level LEVEL 指定log级别,可选参数DEBUG, INFO, WARNING, ERROR, CRITICAL
--ansi (never|always|auto) 控制何时打印ANSI控制字符
-H, --host HOST 指定守护进程主机名Daemon socket
--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 指定工作目录,默认为compose文件所在的目录 (default: the path of the Compose file)
--env-file PATH 指定容器系统环境变量文件

Commands:
build Build or rebuild services
config Validate and view the Compose file
down Stop and remove resources
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
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
start Start services
stop Stop services
top Display the running processes
pause Pause services
unpause Unpause services
up Create and start containers
version Show version information and quit

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
$ docker-compose -v
docker-compose version 1.29.2, build 5becea4c

# -f 指定compose配置文件
# 如果没有指定-f选项,则默认会查找当前目录下的docker-compose.yml和docker-compose.override.yml,后者配置会追加或覆盖前者
$ docker-compose up
# 可以指定多个配置文件,如果配置字段相同,则后者会覆盖前者,如果不同则会合并配置文件,compose按照文件顺序构建配置
$ docker-compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db
# 如下两个配置文件内容,volumes字段相同则后者会覆盖前者,最终结果为/data, admin.yml中的其他字段将会合并到前者
$ more docker-compose.yml
webapp:
image: examples/web
ports:
- "8000:8000"
volumes:
- "/opt"
$ more docker-compose.admin.yml
webapp:
build: .
environment:
- DEBUG=1
volumes:
- "/data"

# -p 指定项目名称,如果没有指定则默认项目名为当前目录名称
$ docker-compose -p demo

# --profile 使用指定配置文件启动
# 如下指定了frontend和debug两个配置文件,而backend和db没有指定配置文件
$ more docker-compose.yml
version: "3.9"
services:
frontend:
image: frontend
profiles: ["frontend"]

phpmyadmin:
image: phpmyadmin
depends_on:
- db
profiles:
- debug

backend:
image: backend

db:
image: mysql
# 没有指定配置文件的服务则默认总是启用,如下命令只会启动backend和db
$ docker-compose up
# 启用指定配置文件,如下命令会启动frontend,backend和db
$ docker-compose --profile frontend up
# 想要启用所有配置文件,则需要手动指定所有profile配置才可以,如下命令会启动所有服务
$ docker-compose --profile frontend --profile debug up