docker-compose -h
打印出docker-compose所有支持的参数flag
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 keys
in v3 files to their non-Swarm equivalent
--env-file PATH Specify an alternate environment file
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
-f 用于指定读取的配置文件
如无指定,则默认寻找当前目录下的docker-compose.yml以及docker-compose.override.yml,如果两个都存在则全部读取
-p 用于指定项目名称
如无指定,则默认为目录名称。规则如下
目录名称_服务名称_编号 其中,如果有.字符的话,则去除掉 ,如zabbix-docker-44_mysql-server_1
环境变量传递
- 本身shell环境变量
- 通过命令行方式,如docker run -e VARIABLE=VALUE …
- 通过配置文件中 environment 进行配置
- 通过文件进行传递,如docker run --env-file=FILE
- 通过配置文件中 env_file 进行配置
- 通过 .env文件,默认docker-compose在执行时,会去读取该文件,从而获取到文件中的环境变量
其中的环境变量的优先级顺序如下:
Compose file
Shell environment variables
Environment file
Dockerfile
Variable is not defined
注意这些环境是可以传递到容器里面去的,可应用于各种应用参数进行配置,从而使每个容器运行的程序具有差异化,响应配置变化。如在启动容器的脚本中,配置一个参数,控制容器运行环境为生产或者开发。
示例
version: '3.5'
services:
appsrv:
image: alpine
entrypoint: ping "${HOST}"
从文件中可以看到有个HOST这个变量,具体值,我们通过文件或命令行的方式进行传递
通过文件.env方式,进行参数传递
.env 文件内容
HOST=127.0.0.1
运行结果
$ docker-compose up
Starting demo_appsrv_1 ... done
Attaching to demo_appsrv_1
appsrv_1 | PING 127.0.0.1 (127.0.0.1): 56 data bytes
appsrv_1 | 64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.061 ms
appsrv_1 | 64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.147 ms
^CGracefully stopping... (press Ctrl+C again to force)
Stopping demo_appsrv_1 ...
Killing demo_appsrv_1 ... done
- PS:这里演示的情况比较简单,主要用来说明,传递环境变量的应用场景。个人认为可以把一个容器模板看成一个函数,通过传递不同的参数可以得到不同的容器,从而使容器可以提供多种不同的功能或者适应不同的场景。因此,一个优秀的docker-compose配置文件或者Dockerfile文件生成的容器,基本不需要对其配置文件或者DOckerfile进行修改,只需要对环境变量参数进行修改即可满足不同场景的需求。
- 变量在容器里面也是可以传递使用
depends_on
- 表示两个服务之间的依赖关系,如下所示,web服务会等待redis以及db服务启动完成后再启动,关闭顺序则相反,会先关闭web服务后,再停止redis以及db服务
version: "3.7"
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
- 这里有点需要注意,假如db启动需要较长时间,只要依赖的服务启动,那么关系就会满足,而不会判断服务是否启动正常
- db作为web依赖,如果启动web,那么db会跟随web自动启动,关闭时则不会随之关闭
docker-compose ps
- 用于查看应用整体状态
$ docker-compose ps
Name Command State Ports
---------------------------------------------
demo_appsrv_1 ping database Up
demo_db_1 sleep 10 Up
command
- 用于覆盖原本容器的command命令
- 示例
$ docker history alpine
IMAGE CREATED CREATED BY SIZE COMMENT
cc0abc535e36 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:36fdc8cb08228a870… 5.59MB
从history来看,默认的alpine的容器启动的第一条命令为/bin/sh
version: '3.5'
services:
appsrv:
image: "${IMAGE}"
command: ["env"]
depends_on:
- db
links:
- db:database
db:
image: alpine
entrypoint: sleep 10
这里使用command将容器启动的第一条命令修改为env
结果如下:
$ docker-compose up
Starting demo_db_1 ... done
Starting demo_appsrv_1 ... done
Attaching to demo_db_1, demo_appsrv_1
appsrv_1 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
appsrv_1 | HOSTNAME=0ddeb85e3941
appsrv_1 | HOME=/root
demo_appsrv_1 exited with code 0
hostname
- 用于定义容器运行时的主机名