示例

Docker Compose 的配置文件 docker-compose.ymlDockerfile 命令多很多。以下是一个 docker-compose.yml 文件示例:

  1. version: "3.7"
  2. services:
  3. redis:
  4. image: redis:alpine
  5. ports:
  6. - "6379"
  7. networks:
  8. - frontend
  9. deploy:
  10. replicas: 2
  11. update_config:
  12. parallelism: 2
  13. delay: 10s
  14. restart_policy:
  15. condition: on-failure
  16. db:
  17. image: postgres:9.4
  18. volumes:
  19. - db-data:/var/lib/postgresql/data
  20. networks:
  21. - backend
  22. deploy:
  23. placement:
  24. constraints: [node.role == manager]
  25. vote:
  26. image: dockersamples/examplevotingapp_vote:before
  27. ports:
  28. - "5000:80"
  29. networks:
  30. - frontend
  31. depends_on:
  32. - redis
  33. deploy:
  34. replicas: 2
  35. update_config:
  36. parallelism: 2
  37. restart_policy:
  38. condition: on-failure
  39. result:
  40. image: dockersamples/examplevotingapp_result:before
  41. ports:
  42. - "5001:80"
  43. networks:
  44. - backend
  45. depends_on:
  46. - db
  47. deploy:
  48. replicas: 1
  49. update_config:
  50. parallelism: 2
  51. delay: 10s
  52. restart_policy:
  53. condition: on-failure
  54. worker:
  55. image: dockersamples/examplevotingapp_worker
  56. networks:
  57. - frontend
  58. - backend
  59. deploy:
  60. mode: replicated
  61. replicas: 1
  62. labels: [APP=VOTING]
  63. restart_policy:
  64. condition: on-failure
  65. delay: 10s
  66. max_attempts: 3
  67. window: 120s
  68. placement:
  69. constraints: [node.role == manager]
  70. visualizer:
  71. image: dockersamples/visualizer:stable
  72. ports:
  73. - "8080:8080"
  74. stop_grace_period: 1m30s
  75. volumes:
  76. - "/var/run/docker.sock:/var/run/docker.sock"
  77. deploy:
  78. placement:
  79. constraints: [node.role == manager]
  80. networks:
  81. frontend:
  82. backend:
  83. volumes:
  84. db-data:

配置文件中,第一层级键主要有:

  • version : 定义模板文件版本(例如: 233.7 );
  • services : 定义整个复杂系统所涉及的一系列服务;
  • volumes : 定义数据卷;
  • networks : 定义网络。

除了 version ,其他几个配置项都有大量的子配置。具体都涉及哪些参数,详见 官方帮助文档。下面对一些常用的配置命令参数做详细介绍。

services 配置

services 的值在 yaml 语法中是一个对象(键值对的集合)。为了控制这些服务,Docker Compuse 定义了大量的的配置项,下面我们介绍这些服务的配置项。

build

定义构建(容器)时的配置项。
build 的值可以是一个字符串或者一个对象。当值为字符串时,其表示构建时的上下文路径。

version: "3.7"
services:
  webapp:
    build: ./dir

或者用对象进行更详细配置:

version: "3.7"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
      args:
        buildno: 1

如果 buildimage 配置同时存在,则 image 的值作为 build 构建镜像的名字。

build: ./dir
image: webapp:tag

build 值为对象时,有如下配置项:

  • context : 包含 Dockerfile 目录的路径,或者是 git 存储库的 url。
  • dockerfile : 指定构建用的 Dockerfile 文件(同时必须定义 context
  • args : 添加构建参数,这些参数只能在构建过程中访问。
  • cache_from : 缓存列表;
  • labels : 将元数据添加到生成的图像中;
  • shm_size : 设置 /dev/shm 分区大小;
  • target : 指定构建阶段,详见 multi-stage build docs 描述。

使用 args 时,我们首先要在 Dockerfile 中定义这些参数:

ARG buildno
ARG gitcommithash

RUN echo "Build number: $buildno"
RUN echo "Based on commit: $gitcommithash"

然后在构建键下指定参数。 您可以传递对象或列表:

build:
  context: .
  args:
    buildno: 1
    gitcommithash: cdc3b19
build:
  context: .
  args:
    - buildno=1
    - gitcommithash=cdc3b19

command

覆盖默认的 command。

command: bundle exec thin -p 3000

它的值也可以是一个列表,与 Dockerfile 中的相关配置类似:

command: ["bundle", "exec", "thin", "-p", "3000"]

configs

container_name

用于自定义容器名称,而不是生成的默认名称。

container_name: my-web-container

depends_on

指明服务间的依赖关系。如果我们定义此配置,docker-compose up 会以依赖顺序启动和停止服务。例如:

version: "3.7"
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

在上述配置中:

  • 当我们用 docker-compose up 启动服务集时,dbredis 会在 web 服务之前启动;
  • 当我们用 docker-compose up web 创建启动 web 服务时,会首先创建和启动 dbredis 服务;
  • 当我们用 docker-compose stop 停止服务集时, web 服务会在 dbredis 服务之前停止。

environment

添加环境变量。 其值可以是数组或字典。

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:
environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET

expose

定义容器暴露的端口,其只能被 link 服务访问。

expose:
 - "3000"
 - "8000"

image

指定容器启动的镜像。其值可以是 repository/tag 或 镜像ID。

image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd

labels

向容器添加元数据。 其值可以是数组或字典。

links

链接到另一个服务中的容器。 指定服务名称和链接别名(SERVICE:ALIAS),或仅指定服务名称。
注:未来可能被删除,建议使用 networks 替代。

networks

要加入的网络,值为第一层级 networks 定义的网络的键。在下面的示例中,提供了三个服务 webworkerdb ,以及两个网络 newlegacy

version: "3.7"

services:
  web:
    image: "nginx:alpine"
    networks:
      - new

  worker:
    image: "my-worker-image:latest"
    networks:
      - legacy

  db:
    image: mysql
    networks:
      new:
        aliases:
          - database
      legacy:
        aliases:
          - mysql

networks:
  new:
  legacy:

ports

暴露端口:

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"

restart

restart: "no"
restart: always
restart: on-failure
restart: unless-stopped

volumes

version: "3.7"
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:

volume 配置

虽然可以在 service 配置中声明,但本部分允许您创建可以跨多个服务重用的命名卷(不依赖于volumes_from),并且可以使用 docker 命令行或 API 轻松检索和检查。有关更多信息,请参阅 docker volume 子命令文档。

以下是一个双服务配置的示例,其中数据库的数据目录作为卷与另一个用作备份服务共享,以便可以定期备份它:

version: "3.7"

services:
  db:
    image: db
    volumes:
      - data-volume:/var/lib/db
  backup:
    image: backup-service
    volumes:
      - data-volume:/var/lib/backup/data

volumes:
  data-volume:

短语法

volumes:
  # Just specify a path and let the Engine create a volume
  # docker 自行创建一个 volume,随机命名
  # 主机存储路径与容器路径的映射由 docker 管理
  - /var/lib/mysql

  # Named volume
  # 添加命名,便于识别
  - datavolume:/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

networks 配置

指定要创建的网络。

version: "3.7"
services:
  web:
    networks:
      hostnet: {}

networks:
  hostnet:
    external: true
    name: host

参考