docker-compose配置文件详解

Compose和Docker兼容性: Compose 文件格式有3个版本,分别为1, 2.x 和 3.x 目前主流的为 3.x 其支持 docker 1.13.0 及其以上的版本 常用参数: version:  services:  build:  context  dockerfile  args  cache_from  labels  shm_size  command:  configs:  cgroup_parent:  container_name:  credential_spec:  deploy:  endpoint_mode:  vip  dnsrr  labels:  mode:  global  replicated  placement:  replicas:  resources:  limits:  cpus: "0.5"  memory: 50M  reservations:  cpus: "0.2"  memory: 20M  restart_policy:  condition:  none  on-failure  any  delay:  max_attempts:  window:  update_config:  parallelism:  delay:  failure_action:  continue  rollback  pause  monitor:  max_failure_ratio:  order:  stop-first  start-first  rollback_config:  parallelism:  delay:  failure_action:  continue  pause  monitor:  max_failure_ratio:  order:  stop-first  start-first  devices:  depends_on:  示例: docker-compose up 以依赖顺序启动服务,下面例子中 redis 和 db 服务在 web 启动前启动 默认情况下使用 docker-compose up web 这样的方式启动 web 服务时,也会启动 redis 和 db 两个服务,因为在配置文件中定义了依赖关系 注意: 支持 docker-compose up 和 docker-compose run 但不支持 docker stack deploy 的子选项 security_opt container_name devices tmpfs stop_signal links cgroup_parent network_mode external_links restart build userns_mode sysctls version: '3' services: web: build: . depends_on: - db - redis redis: image: redis db: image: postgres dns:  dns_search:  tmpfs:  entrypoint:  env_file:  文件格式: RACK_ENV=development environment:  expose:  external_links:  extra_hosts:  healthcheck:  test:  NONE  CMD  CMD-SHELL  interval: 1m30s  timeout: 10s  retries: 3  start_period: 40s  disable: true  image:  init:  isolation:  labels:  links:  logging:  driver:  options:  max-size  max-file  network_mode:  networks:  aliases :  ipv4_address:  ipv6_address:  示例: version: '3.7' services: test: image: nginx:1.14-alpine container_name: mynginx command: ifconfig networks: app_net:  ipv4_address: 172.16.238.10 networks: app_net: driver: bridge ipam: driver: default config: - subnet: 172.16.238.0/24 pid: 'host'  ports:  SHORT 语法格式示例: - "3000"  - "3000-3005"  - "8000:8000"  - "9090-9091:8080-8081" - "127.0.0.1:8001:8001"  - "127.0.0.1:5000-5010:5000-5010" - "6060:6060/udp"  LONG 语法格式示例:(v3.2 新增的语法格式) ports: - target: 80  published: 8080  protocol: tcp  mode: host  secrets:  security_opt:  stop_grace_period:  stop_signal:  sysctls:  ulimits:  userns_mode:  volumes:  SHORT 语法格式示例: volumes: - /var/lib/mysql  - /opt/data:/var/lib/mysql  - ./cache:/tmp/cache  - ~/configs:/etc/configs/:ro  - datavolume:/var/lib/mysql  LONG 语法格式示例:(v3.2 新增的语法格式) 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 read_only: true  volumes: mydata:  restart:  no  always  on-failure  其他选项: privileged: true //设置容器的权限为root hostname: ip domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, working_dir 上面这些选项都只接受单个值和 docker run 的对应参数类似 对于值为时间的可接受的值: 2.5s 10s 1m30s 2h32m 5h34m56s 时间单位: us, ms, s, m, h 对于值为大小的可接受的值: 2b 1024kb 2048k 300m 1gb 单位: b, k, m, g 或者 kb, mb, gb networks:  driver:  bridge  overlay  host  none  driver_opts:  attachable:  ipam:  driver  config  subnet  external:  name:  文件格式示例: 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] 
version: '3' services: redis: image: redis:5.0.7-buster restart: always container_name: my_redis     ports: - "6379:6379" volumes: - ./data:/data   - /etc/localtime:/etc/localtime environment: TIME_ZONE: Asia/Shanghai 

command:执行多条命令
方法一、

  command: /bin/bash -c "cp /app/dtest/config.default.yml /app/config.yml && python -u /app/dtest/tcc.py" 或 command: /bin/bash -c " while true; do sleep 1; done" 

方法二、
1 串行运行
如果这几个命令是没有要求并行运行,则配置如下:

command: - sh - -c - | cmd1 cmd2 cmd3 

如上配置会按顺序执行cmd1,之后再执行cmd2,之后cmd3。对于可以串行的命令,这样即可。

2 并行运行

有时候想配置几个常驻脚本在docker,希望这几个脚本一起跑起来(并行运行),上述1的办法就不行了。
Linux 把命令行扔后台运行的一个办法就是在后面加上 &,记住最后一条命令不要加否则会报错

command: - sh - -c - | cmd1 & cmd2 & cmd3 

docker-compose -f file.yml up -d

YAML 支持所谓的“以60为底的浮点数”,对时间计算很有用。因此2222:22被解释为2222 * 60 + 22,即133342。如果port包含大于60的数字,例如3306:33068080:80,就没有问题,因此不会总是发生此问题,从而使其隐蔽。

使用双引号即可,如下:

services: gogs: ports: - "2222:22" 

原文链接:https://blog.csdn.net/jiangbenchu/article/details/102495531

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享