version
compose的版本
services:
指定需要的容器
webapp:
容器名称
build: ./dir
镜像上下文 Dockerfile路径
build:
context: ./dir
dockfile: mydock
args:
buildno:1
添加构建参数,只能在构建过程中访问
labels:
- "description = webapp"
target: prod
多层构建,指定构建哪一层
CAP_ADD :
- ALL
添加容器拥有的内核权限
CAP_DROP:
- SYS_PTRACE
关闭ptrace权限
cgroup_parent: my-limit
为容器指定父cgroup组,将继承该组的资源限制
command: ["buldle", "exec", "thin", "-p", "3000"]
覆盖容器启动的默认命令
containner_name: "my-self-name"
自定义容器名
depends-on:
- db
- tomcat
设置依赖容器,启动时, db tomcat按顺序先启动,webapp不会等db
tomcat完全启动才启动。停止时,webapp先停止,db tomcat按顺序停止
deploy:
与服务的部署和运行相关的配置,swarm模式下才会用
mode: replicated
服务提供的模式 replicated 复制服务 global 全局服务
replicas: 6
mode为replicated时,配置节点数
endpoint_mode: dnsrr
访问集群的方式
vip 通过虚拟ip
dnsrr 轮询集群IP列表的一个ip
labels:
des: "des"
会被顶层labels覆盖
resources:
limits:
cpus: '0.5',
memory: 50M
服务器资源限制
restart_policy:
condition: none on-failure any
delay
max-attempts
window
rollback_config
parallelism
回滚的容器数
delay
等待时间
failure_action
回滚失败 continue pause
monitor
容器更新以后,持续时间
max_failure_ratio
故障率
order
操作顺序 stop-first start-first
update_config
同rollback_config
devices
设备映射列表
dns 8.8.8.8
dns:
-8.8.8.8
-9.9.9.9
dns_search: exam.com
dns_search:
-a.exam.com
-b.exam.com
entrypoint: ./start.sh
覆盖容器默认的entrypoint
env_file: .env
environment:
布而值需要""括起来
show: "true"
expose:
-“3000”
暴露端口
extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"
主机名映射 /etc/hosts/
检测docker服务是否健康
healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] # 设置检测程序 interval: 1m30s # 设置检测间隔 timeout: 10s # 设置检测超时时间 retries: 3 # 设置重试次数 start_period: 40s # 启动后,多少秒开始启动检测程序
image. 容器运行的镜像
image: redis image: ubuntu:14.04 image: tutum/influxdb image: example-registry.com:4000/postgresql image: a4bc65fd # 镜像id
日志记录配置 logging: driver: json-file options: max-size: "200k" # 单个文件大小为200k max-file: "10" # 最多10个文件
logging: driver: syslog options: syslog-address:
网络模式
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: aliases: - alias1 other-network: aliases: - alias2 networks: some-network: # Use a custom driver driver: custom-driver-1 other-network: # Use a custom driver which takes special options driver: custom-driver-2
非swarm模式下 重启
restart: "no" restart: always restart: on-failure restart: unless-stopped
存储敏感数据
services: mysql: image: mysql environment: MYSQL_ROOT_PASSWORD_FILE: /run/secrets/my_secret secrets: - my_secret secrets: my_secret: file: ./my_secret.txt
设置容器中的内核参数 sysctls: net.core.somaxconn: 1024 net.ipv4.tcp_syncookies: 0 sysctls: - net.core.somaxconn=1024 - net.ipv4.tcp_syncookies=0
在容器内安装一个临时文件系统 tmpfs: /run
将主机的数据卷或着文件挂载到容器里 services: db: image: postgres:latest volumes: - "/localhost/postgres.sock:/var/run/postgres/postgres.sock" - "/localhost/data:/var/lib/postgresql/data"