Docker Compose 配置文件的常用构建参数说明
官方提供了一个 yaml Docker Compose 配置文件的标准例子
version: "3"
services:
redis:
image: redis:alpine
ports:
- "6379"
networks:
- frontend
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- 5000:80
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- 5001:80
networks:
- backend
depends_on:
- db
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
placement:
constraints: [node.role == manager]
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
frontend:
backend:
volumes:
db-data:
1、image
从指定的镜像中启动容器,可以是存储仓库、标签以及镜像 ID
image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd
如果镜像不存在,Compose 会自动拉去镜像
2、network_mode
网络模式,用法类似于 Docke 客户端的 --net 选项,格式为:service:[service name]
可以指定使用服务或者容器的网络
network_mode: “bridge”
network_mode: “host”
network_mode: “none”
network_mode: “service:[service name]”
network_mode: “container:[container name/id]”
3、 networks
加入指定网络
services:
some-service:
networks:
- some-network
- other-network
4、ports
映射端口
1. SHORT 语法
可以使用 HOST:CONTAINER 的方式指定端口,也可以指定容器端口(选择临时主机端口),宿主机会随机映射端口
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”
注意:当使用 HOST:CONTAINER 格式来映射端口时,如果使用的容器端口小于 60 可能会得到错误得结果,因为YAML 将会解析 xx:yy 这种数字格式为 60 进制,所以建议采用字符串格式。
2. LONG 语法
LONG 语法支持 SHORT 语法不支持的附加字段
target:容器内的端口
published:公开的端口
protocol: 端口协议(tcp 或 udp)
mode:通过host 用在每个节点还是哪个发布的主机端口或使用 ingress 用于集群模式端口进行平衡负载,
ports:• target: 80
published: 8080
protocol: tcp
mode: host
5、volumes
挂载一个目录或者一个已存在的数据卷容器,可以直接使用 HOST:CONTAINER 这样的格式,或者使用 HOST:CONTAINER:ro 这样的格式,后者对于容器来说,数据卷是只读的,这样可以有效保护宿主机的文件系统
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:
Compose 的数据卷指定路径可以是相对路径,使用 . 或者 … 来指定相对目录。
数据卷的格式可以是下面多种形式:
volumes:
#只是指定一个路径,Docker 会自动在创建一个数据卷(这个路径是容器内部的)。
- /var/lib/mysql
使用绝对路径挂载数据卷
- /opt/data:/var/lib/mysql
以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。
- ./cache:/tmp/cache
使用用户的相对路径(~/ 表示的目录是 /home/<用户目录>/ 或者 /root/)。
- ~/configs:/etc/configs/:ro
已经存在的命名的数据卷。
- datavolume:/var/lib/mysql
如果你不使用宿主机的路径,可以指定一个 volume_driver
volume_driver: mydriver
1. SHORT 语法
可以选择在主机(HOST:CONTAINER)或访问模式(HOST:CONTAINER:ro)上指定路径。
可以在主机上挂载相对路径,该路径相对于正在使用的 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
2. LONG 语法
LONG 语法有些附加字段
type:安装类型,可以为 volume、bind 或 tmpfs
source:安装源,主机上用于绑定安装的路径或定义在顶级 volumes密钥中卷的名称 ,不适用于 tmpfs 类型安装。
target:卷安装在容器中的路径
read_only:标志将卷设置为只读
bind:配置额外的绑定选项
propagation:用于绑定的传播模式
volume:配置额外的音量选项
nocopy:创建卷时禁止从容器复制数据的标志
tmpfs:配置额外的 tmpfs 选项
size:tmpfs 的大小,以字节为单位
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/staticnetworks:
webnet:volumes:
mydata:
6、 volumes_from
从其它容器或者服务挂载数据卷,可选的参数是 :ro 或 :rw,前者表示容器只读,后者表示容器对数据卷是可读可写的(默认情况为可读可写的)。
volumes_from:
- service_name
- service_name:ro
- container:container_name
- container:container_name:rw
7、depends_on
此选项解决了启动顺序的问题
在使用 Compose 时,最大的好处就是少打启动命令,但是一般项目容器启动的顺序是有要求的,如果直接从上到下启动容器,必然会因为容器依赖问题而启动失败。例如在没启动数据库容器的时候启动了应用容器,这时候应用容器会因为找不到数据库而退出,为了避免这种情况我们需要加入一个标签,就是 depends_on,这个标签解决了容器的依赖、启动先后的问题。
指定服务之间的依赖关系,有两种效果
docker-compose up 以依赖顺序启动服务,下面例子中 redis 和 db 服务在 web 启动前启动
docker-compose up SERVICE 自动包含 SERVICE 的依赖性,下面例子中,例如下面容器会先启动 redis 和 db
两个服务,最后才启动 web 服务:
version: ‘3’
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
注意的是,默认情况下使用 docker-compose up web 这样的方式启动 web 服务时,也会启动 redis 和 db 两个服务,因为在配置文件中定义了依赖关系
8、deploy
指定与部署和运行服务相关的配置
version: ‘3’
services:
redis:
image: redis:alpine
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
这里有几个子选项
1. endpoint_mode
指定连接到群组外部客户端服务发现方法
endpoint_mode:vip :Docker 为该服务分配了一个虚拟 IP(VIP),作为客户端的 “前端“ 部位用于访问网络上的服务。
endpoint_mode: dnsrr : DNS轮询(DNSRR)服务发现不使用单个虚拟 IP。Docker为服务设置 DNS 条目,使得服务名称的 DNS 查询返回一个 IP 地址列表,并且客户端直接连接到其中的一个。如果想使用自己的负载平衡器,或者混合 Windows 和 Linux 应用程序,则 DNS 轮询调度(round-robin)功能就非常实用。
version: “3.3”
services:
wordpress:
image: wordpress
ports:
- 8080:80
networks:
- overlay
deploy:
mode: replicated
replicas: 2
endpoint_mode: vipmysql:
image: mysql
volumes:
- db-data:/var/lib/mysql/data
networks:
- overlay
deploy:
mode: replicated
replicas: 2
endpoint_mode: dnsrrvolumes:
db-data:networks:
overlay:
相关信息:Swarm 模式 CLI 命令 、Configure 服务发现
2.labels
指定服务的标签,这些标签仅在服务上设置。
version: “3”
services:
web:
image: web
deploy:
labels:
com.example.description: “This label will appear on the web service”
通过将 deploy 外面的 labels 标签来设置容器上的 labels
version: “3”
services:
web:
image: web
labels:
com.example.description: “This label will appear on all containers for the web service”
3.mode
global:每个集节点只有一个容器
replicated:指定容器数量(默认)
version: ‘3’
services:
worker:
image: dockersamples/examplevotingapp_worker
deploy:
mode: global
4. placement
指定 constraints 和 preferences
version: ‘3’
services:
db:
image: postgres
deploy:
placement:
constraints:
- node.role == manager
- engine.labels.operatingsystem == ubuntu 14.04
preferences:
- spread: node.labels.zone
5.replicas
如果服务是 replicated(默认),需要指定运行的容器数量
version: ‘3’
services:
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 6
6. resources
配置资源限制
version: ‘3’
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: ‘0.50’
memory: 50M
reservations:
cpus: ‘0.25’
memory: 20M
此例子中,redis 服务限制使用不超过 50M 的内存和 0.50(50%)可用处理时间(CPU),并且 保留 20M 了内存和 0.25 CPU时间
7. restart_policy
配置容器的重新启动,代替 restart
condition:值可以为 none 、on-failure 以及 any(默认)
delay: 尝试重启的等待时间,默认为 0
max_attempts:在放弃之前尝试重新启动容器次数(默认:从不放弃)。如果重新启动在配置中没有成功 window,则此尝试不计入配置max_attempts 值。例如,如果 max_attempts 值为 2,并且第一次尝试重新启动失败,则可能会尝试重新启动两次以上。
windows:在决定重新启动是否成功之前的等时间,指定为持续时间(默认值:立即决定)。
version: “3”
services:
redis:
image: redis:alpine
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
8. update_config
配置更新服务,用于无缝更新应用(rolling update)
parallelism:一次性更新的容器数量
delay:更新一组容器之间的等待时间。
failure_action:如果更新失败,可以执行的的是 continue、rollback 或 pause (默认)
monitor:每次任务更新后监视失败的时间(ns|us|ms|s|m|h)(默认为0)
max_failure_ratio:在更新期间能接受的失败率
order:更新次序设置,top-first(旧的任务在开始新任务之前停止)、start-first(新的任务首先启动,并且正在运行的任务短暂重叠)(默认 stop-first)
version: ‘3.4’
services:
vote:
image: dockersamples/examplevotingapp_vote:before
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
order: stop-first
不支持 Docker stack desploy 的几个子选项
build、cgroup_parent、container_name、devices、tmpfs、external_links、inks、network_mode、restart、security_opt、stop_signal、sysctls、userns_mode