Define application stacks built using multiple containers, services, and swarm configurations.
GitHub: https://github.com/docker/compose
install
Docker CE for Windows 10 、Docker CE for Mac 自带 docker-compose
,官方建议随 Docker 版本升级。
Linux 请在 GitHub releases 处下载二进制文件,移入 PATH
并赋予可执行权限。
或者执行以下命令进行下载安装。
1 | $ DOCKER_COMPOSE_VERSION=1.18.0 |
或者通过 Python
包管理工具 pip
安装。
Command-line completion
fish
~/.config/fish/completions
1 | $ wget https://raw.githubusercontent.com/docker/compose/master/contrib/completion/fish/docker-compose.fish |
bash
官方文档:https://docs.docker.com/compose/completion/
Compose file reference
build
1 | version: '3' |
Dockerfile
中包含变量
1 | ARG buildno |
cap_add, cap_drop
没用过,不了解。
Add or drop container capabilities. See man 7 capabilities
for a full list.
1 | cap_add: |
command
1 | command: bundle exec thin -p 3000 |
configs
3.3
1 | version: "3.3" |
1 | version: "3.3" |
cgroup_parent
Specify an optional parent cgroup for the container.
1 | cgroup_parent: m-executor-abcd |
container_name
不建议使用,此项配置的话,服务将不能扩展。
1 | container_name: my-web-container |
credential_spec
没用过,不了解。
3.3
仅用于 Windows 容器。
deploy
仅用于 Swarm mode
1 | version: '3' |
endpoint_mode
3.3
1 | deploy: |
mode
https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/#replicated-and-global-services
1 | deploy: |
placement
1 | deploy: |
resources
资源限制
1 | deploy: |
restart_policy
1 | version: "3" |
update_config
1 | version: '3.4' |
docker stack deploy
不支持以下参数
build
cgroup_parent
container_name
devices
tmpfs
external_links
links
network_mode
security_opt
stop_signal
sysctls
userns_mode
devices
List of device mappings. Uses the same format as the –device docker client create option.
1 | devices: |
depends_on
保证依赖的服务完全启动之后才启动 https://docs.docker.com/compose/startup-order/
依赖关系
1 | version: '3' |
dns
1 | dns: 8.8.8.8 |
dns_search
1 | dns_search: example.com |
tmpfs
1 | tmpfs: /run |
entrypoint
入口文件
1 | entrypoint: /code/entrypoint.sh |
env_file
从文件读取变量写入镜像 环境变量
1 | env_file: .env |
若变量重复,后边文件的变量会覆盖后边的。
env
文件内容举例
1 | # 支持 # 号注释 |
environment
设置环境变量
1 | environment: |
expose
内部暴露端口
1 | expose: |
external_links
链接外部容器。不建议使用,建议通过网络进行连接!
CONTAINER:ALIAS
1 | external_links: |
extra_hosts
1 | extra_hosts: |
在容器内 /etc/hosts
写入下面的内容
1 | 162.242.195.82 somehost |
healthcheck
健康检查
1 | healthcheck: |
1 | # Hit the local web app |
image
1 | image: redis |
isolation
Specify a container’s isolation technology. On Linux, the only supported value is default
. On Windows, acceptable values are default
, process
and hyperv
.
labels
1 | labels: |
links
不建议使用!
1 | web: |
logging
日志配置
1 | logging: |
1 | driver: "json-file" |
1 | options: |
1 | services: |
network_mode
1 | network_mode: "bridge" |
networks
1 | services: |
aliases
1 | services: |
1 | version: '2' |
ipv4_address ipv6_address
1 | version: '2.1' |
pid
1 | pid: "host" |
ports
1 | ports: |
3.2 开始支持长格式
1 | ports: |
secrets
1 | version: "3.1" |
长格式
1 | version: "3.1" |
security_opt
1 | security_opt: |
stop_grace_period
1 | stop_grace_period: 1s |
stop_signal
1 | stop_signal: SIGUSR1 |
sysctls
Kernel parameters to set in the container. You can use either an array or a dictionary.
1 | sysctls: |
ulimits
Override the default ulimits for a container. You can either specify a single limit as an integer or soft/hard limits as a mapping.
1 | ulimits: |
userns_mode
Disables the user namespace for this service, if Docker daemon is configured with user namespaces. See dockerd for more information.
1 | userns_mode: "host" |
volumes
1 | version: "3.2" |
1 | volumes: |
1 | version: "3.2" |
restart
1 | restart: "no" |
domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, working_dir
1 | user: postgresql |
Specifying durations
1 | 2.5s |
Volume configuration reference
1 | version: "3" |
1 | driver: foobar |
1 | driver_opts: |
1 | volumes: |
1 | labels: |
1 | volumes: |
name
1 | version: '3.4' |
1 | version: '3.4' |
Network configuration reference
1 | driver: overlay |
host OR none
用于 docker stack
,如果使用 docker-compose
请使用 network_mode
。
类似于 docker run --net=host
1 | services: |
类似于 docker run --net=none
1 | services: |
driver_opts
1 | driver_opts: |
attachable
1 | networks: |
ipam
1 | ipam: |
name
3.5
1 | version: '3.5' |
1 | version: '3.5' |
configs configuration reference
1 | configs: |
3.5
1 | configs: |
secrets configuration reference
1 | secrets: |
3.5
1 | secrets: |
Variable substitution
1 | db: |
从 .env
文件或系统变量中读取变量,来替换 compose 文件中的变量。
docker stack deploy
不支持变量读取。
$VAR
${VAR}
这两种格式都支持。
${VARIABLE:-default}
如果 VARIABLE
被 unset
或为空 (empty
) 时设置为 default
。
${VARIABLE-default}
如果 VARIABLE
被 unset
时设置为 default
。
使用 $$
避免解析变量
1 | web: |
Extension fields
1 | version: '2.1' |
1 | logging: |
1 | version: '2.1' |
1 | version: '2.1' |