service configuration
build
构建镜像时候的一个选项
version: '3'
services:
webapp:
build: ./dir
也可以指定路径,dokerfile文件,参数
version: '3'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
构建指定镜像
build: ./dir
image: webapp:tag
context
-
要么是包含 dockerfile 的目录,或者是 git 仓库的 url
-
当提供的是一个相对路径,是相对于
docker-compose.yml
文件的相对路径,这个目录也是发送到 docker 守护线程构建 -
构建并且标记生成名称然后使用该镜像
build: context: ./dir
dockerfile
指定 dockerfile 文件名称并且需要指定构建路径
build:
context: .
dockerfile: Dockerfile-alternate
args
添加构建参数,启动容器的时候当做环境变量
添加参数在 dockerfile
ARG buildno
ARG gitcommithash
指定参数在 build 下面,可以是 list 或者是 mapping
build:
context: .
args:
buildno: 1
gitcommithash: cdc3b19
build:
context: .
args:
- buildno=1
- gitcommithash=cdc3b19
cache_from
版本至少3.2
用于缓存解析镜像列表
build:
context: .
cache_from:
- alpine:latest
- corp/web_app:3.14
labels
版本至少3.3
使用docker label
添加结果元数据,可以使用lsit 或mapping
使用反向 dns 来防止标签与其他软件的标签冲突
build:
context: .
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
build:
context: .
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
shm_size
版本至少3.5
指定容器/dev/shm的大小
build:
context: .
shm_size: '2gb'
build:
context: .
shm_size: 10000000
target
根据对应的 dockerfile 构建指定 stage
build:
context: .
target: prod
cap_add, cap_drop
这个选项在 swarm 模式下部署忽略
添加或删除容器功能
cap_add:
- ALL
cap_drop:
- NET_ADMIN
- SYS_ADMIN
command
重写默认的command
command: bundle exec thin -p 3000
command: ["bundle", "exec", "thin", "-p", "3000"]
configs
必须配置顶级配置
使每个服务基于授予对配置的访问权限
short syntax
short syntax
只指定配置名称,授予容器对配置的访问权限,并且挂在到容器中的/<config_name>
下,源名称和目标安装点都设置为配置名称
version: "3.3"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- my_config
- my_other_config
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
long syntax
long syntax
提供了在服务的任务容器中如何创建配置的更多粒度
-
source
docker中存在的配置名称 -
target
要在service容器中挂在路径和名称,如果没有指定默认/<source>
-
uid
和gid
:在service容器中拥有已装入的配置文件的数字UID或GID,没有指定在linux
上默认值是0,windows 不支持 -
mode:设置权限,没有配置默认值是0444,只能读,
version: "3.3" services: redis: image: redis:latest deploy: replicas: 1 configs: - source: my_config target: /redis_config uid: '103' gid: '103' mode: 0440 configs: my_config: file: ./my_config.txt my_other_config: external: true
cgroup_parent
容器分组在swarm
下忽略此配置
container_name
指定一个自定义容器名称,不生成默认名称
deploy
指定部署并且运行的容器
version: '3'
services:
redis:
image: redis:alpine
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
endpoint_mode
只适用于3.3版本
为连接到swarm
的外部客户端指定服务发现方法。
-
endpoint_mode: vip
Docker为服务分配虚拟IP(VIP),作为客户端到达网络服务的“前端”。 Docker在客户端和服务的可用工作节点之间路由请求,而无需客户端知道有多少节点参与服务或其IP地址或端口。 (这是默认设置。)
-
endpoint_mode: dnsrr
DNS循环(DNSRR)服务发现不使用单个虚拟IP。 Docker为服务设置DNS条目,以便服务名称的DNS查询返回IP地址列表,客户端直接连接到其中一个。 如果您要使用自己的负载均衡器,或者对于混合Windows和Linux应用程序,DNS循环法非常有用。
version: "3.3" services: wordpress: image: wordpress ports: - "8080:80" networks: - overlay deploy: mode: replicated replicas: 2 endpoint_mode: vip mysql: image: mysql volumes: - db-data:/var/lib/mysql/data networks: - overlay deploy: mode: replicated replicas: 2 endpoint_mode: dnsrr volumes: db-data: networks: overlay:
labels
指定服务的标签。 这些标签仅在服务上设置,而不是在服务的任何容器上设置。
version: "3"
services:
web:
image: web
deploy:
labels:
com.example.description: "This label will appear on the web service"
mode
global
或者replicated
,默认值replicated
placement
指定容器或者配置
version: '3.3'
services:
db:
image: postgres
deploy:
placement:
constraints:
- node.role == manager
- engine.labels.operatingsystem == ubuntu 14.04
preferences:
- spread: node.labels.zone
replicas
如果服务是replicated
,指定在任何时间内容器运行数量
version: '3'
services:
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 6
resources
配置资源限制
version: '3'
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M
restart_policy
配置当容器退出时是否或者如何重启容器
-
condition
不重启,失败,任何情况 -
delay
从起尝试等待时间,默认值是0,不等待 -
max_attempts
在放弃之前尝试容器的次数,默认值一直尝试,不放弃,在配置窗口重启没有成功,此次尝试不会计入配置的max_attempts之内version: "3" services: redis: image: redis:alpine deploy: restart_policy: condition: on-failure delay: 5s max_attempts: 3 window: 120s
rollback_config
至少需要3.7版本
配置更新失败如何回滚
-
parallelism
容器的回滚数量,设置0全部容器 -
delay
每个容器组容器之间的等待 -
failure_action
回滚失败操作,继续或者暂停,默认是暂停 -
monitor
每次更新监控任务失败持续时间 -
max_failure_ratio
回滚期间最大失败率默认是0 -
order
回滚期间的操作顺序。 One ofstop-first
(旧任务在启动新任务之前停止)或start-first
(首先启动新任务,并且正在运行的任务暂时重叠)默认stop-firs)order
支持的版本至少是3.4version: '3.4' services: vote: image: dockersamples/examplevotingapp_vote:before depends_on: - redis deploy: replicas: 2 update_config: parallelism: 2 delay: 10s order: stop-first
update_config
配置服务应如何更新
-
parallelism
一次容器更新数量 -
delay
更新一组容器之间等待时间 -
failure_action更新失败操作,继续或者暂停,默认是暂停
-
monitor
每次更新监控任务失败持续时间 -
max_failure_ratio
更新期间最大失败率默认是0 -
order
更新期间的操作顺序。 One ofstop-first
(旧任务在启动新任务之前停止)或start-first
(首先启动新任务,并且正在运行的任务暂时重叠)默认stop-firs)order
支持的版本至少是3.4version: '3.4' services: vote: image: dockersamples/examplevotingapp_vote:before depends_on: - redis deploy: replicas: 2 update_config: parallelism: 2 delay: 10s order: stop-first
devices
swarm 模式不支持
设备映射列表
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
depends_on
服务依赖关系
version: '3'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
dns
自定义DNS服务,可以是单值或列表
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
dns_search
自定义DNS查询域名,可以是单值或列表
dns_search: example.com
dns_search:
- dc1.example.com
- dc2.example.com
tmpfs
在容器内安装临时文件系统。 可以是单个值或列表
tmpfs: /run
tmpfs:
- /run
- /tmp
entrypoint
覆盖默认入口点
entrypoint: /code/entrypoint.sh
entrypoint:
- php
- -d
- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
- -d
- memory_limit=-1
- vendor/bin/phpunit
env_file
添加环境变量文件
env_file: .env
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
environment
添加环境变量,boolean值需要放在引号之内
environment:
RACK_ENV: development
SHOW: 'true'
SESSION_SECRET:
environment:
- RACK_ENV=development
- SHOW=true
- SESSION_SECRET
expose
暴露端口而不将它们发布到主机,它们只能被链接服务访问,只能指定内部端口
expose:
- "3000"
- "8000"
external_links
链接docker-compose.yml
之外的容器甚至docker-composer
之外的容器,尤其是对外提供共享或公共服务
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
extra_hosts
添加主机映射
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
healthcheck
配置健康检查
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
start_period
版本至少3.4
image
指定镜像创建容器
image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd
init
至少3.7版本
在容器内运行init,转发信号并重新获取进程。 设置布尔值以使用默认init,或指定自定义路径的路径。
version: '3.7'
services:
web:
image: alpine:latest
init: true
version: '2.2'
services:
web:
image: alpine:latest
init: /usr/libexec/docker-init
labels
使用Docker标签向容器添加元数据。 您可以使用数组或字典。
建议您使用反向DNS表示法来防止标签与其他软件使用的标签冲突。
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
links
链接到另一个服务中的容器docker-compose.yml
内的容器,别名当做主机名
web:
links:
- db
- db:database
- redis
logging
配置服务日志,驱动名称就是服务名称
driver: "json-file"
driver: "syslog"
driver: "none"
使用options键为日志记录驱动程序指定日志记录选项
driver: "syslog"
options:
syslog-address: "tcp://192.168.0.42:123"
默认驱动是json-file
,可以限制存储日志
options:
max-size: "200k"
max-file: "10"
services:
some-service:
image: some-service
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
network_mode
网络模式
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
networks
加入网络需要配置在顶级
services:
some-service:
networks:
- some-network
- other-network
aliases
网络上此服务的别名(备用主机名)。 同一网络上的其他容器可以使用服务名称或此别名连接到其中一个服务的容器。
services:
some-service:
networks:
some-network:
aliases:
- alias1
- alias3
other-network:
aliases:
- alias2
ipv4_address,ipv6_address
在加入网络时为此服务指定容器的静态IP地址,顶级网络部分中的相应网络配置必须具有包含每个静态地址的子网配置的ipam块。 如果需要IPv6寻址,则必须设置enable_ipv6选项
version: '2.1'
services:
app:
image: busybox
command: ifconfig
networks:
app_net:
ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10
networks:
app_net:
driver: bridge
enable_ipv6: true
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24
-
subnet: 2001:3984:3989::/64
pid
将PID模式设置为主机PID模式。 这打开了容器和主机操作系统之间的PID地址空间共享。 使用此标志启动的容器可以访问和操作裸机计算机命名空间中的其他容器,反之亦然
ports
暴露端口
short syntax
ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"
- "6060:6060/udp"
long syntax
target
: 外部端口published
:容器发布端口protocol
: 协议mode
: 主机用于在每个节点上发布主机端口,或者用于群集模式端口的入口以进行负载平衡
ports:
- target: 80
published: 8080
protocol: tcp
mode: host
secrets
使用服务加密配置,基于每个服务授予对机密的访问权限
short syntax
这将授予容器对秘密的访问权限,并将其安装在容器内的/ run / secrets / <secret_name>中。 源名称和目标安装点都设置为机密名称
version: "3.1"
services:
redis:
image: redis:latest
deploy:
replicas: 1
secrets:
- my_secret
- my_other_secret
secrets:
my_secret:
file: ./my_secret.txt
my_other_secret:
external: true
long syntax
-
source
docker 中已经存在的加密名称 -
target
要在服务的任务容器中的/run/secrets/
中挂载的文件的名称。 如果未指定,则默认为source -
uid
andgid
: 在服务的任务容器中拥有/run/secrets/
文件的数字UID或GID。 如果未指定,则默认为“0” -
mode
文件权限,默认是0444,只读version: "3.1" services: redis: image: redis:latest deploy: replicas: 1 secrets: - source: my_secret target: redis_secret uid: '103' gid: '103' mode: 0440 secrets: my_secret: file: ./my_secret.txt my_other_secret: external: true
security_opt
覆盖每个容器的默认标签方案
security_opt:
- label:user:USER
- label:role:ROLE
stop_grace_period
指定在发送SIGKILL之前,如果它未处理SIGTERM(或使用stop_signal指定了任何停止信号),则尝试停止容器时要等待多长时间。 指定为持续时间
stop_grace_period: 1s
stop_grace_period: 1m30s
stop_signal
swarm 中无效
置替代信号以停止容器。 默认情况下,stop使用SIGTERM。 使用stop_signal设置替代信号会导致停止发送该信号
stop_signal: SIGUSR1
sysctls
swarm 模式无效
要在容器中设置的内核参数。 您可以使用数组或字典
sysctls:
net.core.somaxconn: 1024
net.ipv4.tcp_syncookies: 0
sysctls:
- net.core.somaxconn=1024
- net.ipv4.tcp_syncookies=0
ulimits
覆盖容器的默认ulimits。 您可以将单个限制指定为整数,也可以将软/硬限制指定为映射。
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
userns_mode
如果Docker守护程序配置了用户名称空间,则禁用此服务的用户名称空间
userns_mode: "host"
volumes
宿主目录挂在到容器主机目录下
version: "3.2"
services:
web:
image: nginx:alpine
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
db:
image: postgres:latest
volumes:
- "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
- "dbdata:/var/lib/postgresql/data"
volumes:
mydata:
dbdata:
short syntax
您可以在主机上安装相对路径,该路径相对于正在使用的Compose配置文件的目录进行扩展
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Specify an absolute path mapping
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file
- ./cache:/tmp/cache
# User-relative path
- ~/configs:/etc/configs/:ro
# Named volume
- datavolume:/var/lib/mysql
long syntax
-
type
挂载类型volume
,bind
or `tmpfs -
source
挂载到容器上的宿主主机的路径 -
target
挂载到的容器主机的路径 -
read_only
标记是否只读 -
bind
绑定选项propagation
只用在bind
类型
-
volume
配置volume
选项nocopy
用于在创建卷时禁用从容器复制数据
-
tmpfs
size tmpfs
挂载的大小
-
consistency
一致性要求version: "3.2" services: web: image: nginx:alpine ports: - "80:80" volumes: - type: volume source: mydata target: /data volume: nocopy: true - type: bind source: ./static target: /opt/app/static networks: webnet: volumes: mydata:
restart
no
是默认的重启策略,并且在任何情况下都不会重新启动容器。 指定always
时,容器始终重新启动。 如果退出代码指示出现故障错误,则on-failure
策略将重新启动容器。
restart: "no"
restart: always
restart: on-failure
restart: unless-stopped
network configuration
顶级networks
可以指定需要创建的network
driver
指定网络使用哪个驱动,默认使用bridge
,swarm
默认使用overlay
driver: overlay
bridge
docker默认单机使用网桥模式
overlay
overlay
创建网络在多节点上
host or none
使用主机的网络堆栈,或不使用网络。 相当于docker run --net = host或docker run --net = none。 仅在使用docker stack命令时使用。 如果使用docker-compose命令,请改用network_mode
使用内置网络(如host和none)的语法略有不同。 定义名为host或none(Docker已自动创建)的外部网络以及Compose可以使用的别名(在这些示例中为hostnet或nonet),然后使用别名授予对该网络的服务访问权限
services:
web:
...
networks:
hostnet: {}
networks:
hostnet:
external: true
name: host
services:
web:
...
networks:
nonet: {}
networks:
nonet:
external: true
name: none
driver_opts
将选项列表指定为键值对,以传递给此网络的驱动程序
driver_opts:
foo: "bar"
baz: 1
attachable
当驱动使用的是overlay
才使用,如果设置成true
,单独网络可以访问到此服务
networks:
mynet1:
driver: overlay
attachable: true
enable_ipv6
启用ipv6
网络
ipam
指定自定义IPAM
driver
自定义IPAM驱动程序,而不是默认值config
包含零个或多个配置块的列表subnet
CIDR格式的子网,代表网段
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
internal
默认情况下,Docker还将桥接网络连接到它以提供外部连接。 如果要创建外部隔离的覆盖网络,可以将此选项设置为true
labels
使用Docker标签向容器添加元数据。 您可以使用数组或字典
建议您使用反向DNS表示法来防止标签与其他软件使用的标签冲突
labels:
com.example.description: "Financial transaction network"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Financial transaction network"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
external
如果设置为true,则指定已在Compose之外创建此网络。 docker-compose up不会尝试创建它,如果它不存在则会引发错误
external不能与其他网络配置键(driver,driver_opts,ipam,internal)一起使用
在下面的示例中,proxy是通往外部世界的网关。 而不是尝试创建一个名为[projectname] _outside的网络,Compose寻找一个简单地在外面调用的现有网络,并将代理服务的容器连接到它
version: '2'
services:
proxy:
build: ./proxy
networks:
- outside
- default
app:
build: ./app
networks:
- default
networks:
outside:
external: true
还可以在Compose文件中单独指定用于引用网络名称的网络名称
networks:
outside:
external:
name: actual-name-of-network
name
为此网络设置自定义名称。 名称字段可用于引用包含特殊字符的网络。 该名称按原样使用,不会使用堆栈名称作为范围
version: '3.5'
networks:
network1:
name: my-app-net
它也可以与外部属性一起使用
version: '3.5'
networks:
network1:
external: true
name: my-app-net
configs configuration
顶级配置声明定义或引用可以授予此堆栈中的服务的配置。 配置源是文件或外部
file
使用指定路径上的文件内容创建配置external
如果设置为true
,则指定已创建此配置,Docker不会尝试创建它,如果它不存在,则会发生配置未找到错误name
Docker中配置对象的名称, 此字段可用于引用包含特殊字符的配置, 该名称按原样使用,不会使用堆栈名称作为范围 以3.5版文件格式引入
configs:
my_first_config:
file: ./config_data
my_second_config:
external: true
secrets configuration
顶级加密声明定义或引用可以授予此堆栈中的服务的机密, 加密的来源是文件或外部
file
使用指定路径上的文件内容创建密钥external
如果设置为``true`,则指定已创建此密钥, Docker不会尝试创建它,如果它不存在,则会发生未找到机密的错误name
Docker中的秘密对象的名称, 此字段可用于引用包含特殊字符的机密, 该名称按原样使用,不会使用堆栈名称作为范围, 以3.5版文件格式引入
secrets:
my_first_secret:
file: ./secret_data
my_second_secret:
external: true
secrets:
my_first_secret:
file: ./secret_data
my_second_secret:
external:
name: redis_secret