Docker之compose

Compose 是用于定义和运行多容器 Docker 应用程序的工具,通过 Compose 可以使用 YML 文件来配置应用程序需要的所有服务,然后使用一个命令就可以从 YML 文件配置中创建并启动所有服务。

Compose 使用的步骤:

  • 使用 Dockerfile 定义应用程序的环境
  • 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行
  • 执行 docker-compose up 命令来启动并运行整个应用程序

 yml配置指令

  • version

指定本 yml 依从的 compose 哪个版本制定的

  • build

指定为构建镜像上下文路径:

例如 webapp 服务,指定为从上下文路径 ./dir/Dockerfile 所构建的镜像:

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

或者作为具有在上下文指定的路径的对象,以及可选的 Dockerfile 和 args:

version: "3.7"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
      args:
        buildno: 1
      labels:
        - "com.example.description=Accounting webapp"
        - "com.example.department=Finance"
        - "com.example.label-with-empty-value"
      target: prod
  1. context:上下文路径
  2. dockerfile:指定构建镜像的 Dockerfile 文件名
  3. args:添加构建参数,这是只能在构建过程中访问的环境变量
  4. labels:设置构建镜像的标签
  5. target:多层构建,可以指定构建哪一层
  • cap_add、cap_drop

添加或删除容器拥有的宿主机的内核功能

cap_add:
  - ALL # 开启全部权限

cap_drop:
  - SYS_PTRACE # 关闭 ptrace权限
  • cgroup_parent

为容器指定父 cgroup 组,意味着将继承该组的资源限制

cgroup_parent: m-executor-abcd
  • command

覆盖容器启动的默认命令

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

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

container_name: my-web-container
  • depends_on

设置依赖关系,以依赖性顺序启动服务

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

先启动 db 和 redis ,然后才启动 web

  • devices

指定设备映射列表

devices:
  - "/dev/ttyUSB0:/dev/ttyUSB0"
  • 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
  • entrypoint

覆盖容器默认的 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

添加环境变量。可以使用数组或字典、任何布尔值,布尔值需要用引号引起来,以确保 YML 解析器不会将其转换为 True 或 False。

environment:
  RACK_ENV: development
  SHOW: 'true'
  • expose

暴露端口,但不映射到宿主机,只被连接的服务访问,仅可以指定内部端口为参数

expose:
 - "3000"
 - "8000"
  • extra_hosts

添加主机名映射。类似 docker client --add-host

extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"

以上会在此服务容器内部中的 /etc/hosts 创建一个具有 ip 地址和主机名的映射关系:

162.242.195.82  somehost
50.31.209.229   otherhost
  • image

指定容器运行的镜像。以下格式都可以:

image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd # 镜像id
  • logging

服务的日志记录配置。

driver:指定服务容器的日志记录驱动程序,默认值为json-file。有以下三个选项:

driver: "json-file"
driver: "syslog"
driver: "none"

仅在 json-file 驱动程序下,可以使用以下参数,限制日志得数量和大小:

logging:
  driver: json-file
  options:
    max-size: "200k" # 单个文件大小为200k
    max-file: "10" # 最多10个文件

当达到文件限制上限,会自动删除旧的文件

syslog 驱动程序下,可以使用 syslog-address 指定日志接收地址:

logging:
  driver: syslog
  options:
    syslog-address: "tcp://192.168.0.42:123"
  • network_mode

设置网络模式

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
  • volumes

将宿主机的数据卷或文件挂载到容器里

version: "3.7"
services:
  db:
    image: postgres:latest
    volumes:
      - "/localhost/postgres.sock:/var/run/postgres/postgres.sock"
      - "/localhost/data:/var/lib/postgresql/data"

  

原文地址:https://www.cnblogs.com/xi-jie/p/14923504.html