Docker Compose 配置文件常用指令

Docker Compose 配置文件常用指令


YAML文件格式及编写注意事项

  • YAML是一种标记语言很直观的数据序列化格式,可读性高。类似于XML数据描述语言,语法比XML简单的很多。
  • YAML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。

YAML文件格式注意事项:

  • 1. 不支持制表符tab键缩进,需要使用空格缩进
  • 2. 通常开头缩进2个空格
  • 3. 字符后缩进1个空格,

# 构建镜像上下文路径

build

build: ./dir

build:
  context: ./dir
  dockerfile: Dockerfile-alternate
  args:
    buildno: 1
View Code
build: ./dir
image: webapp:tag
View Code

# 指定Dockefile文件名

dockerfile

build:
  context: .
  dockerfile: Dockerfile-alternate
View Code

# 来自镜像

image

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

# 构建参数。在Dockerfile中指定的参数

args

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

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

# 覆盖默认命令

command

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

# 自定义容器名称。如果自定义名称,则无法将服务scale到1容器之外

container_name 

container_name: my-web-container
View Code

# 指定与部署和运行相关的配置。限版本3

deploy

version: '3'
services:
  redis:
    image: redis:alpine
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
View Code

# 服务之间的依赖,控制服务启动顺序。正常是按顺序启动服务

depends_on

version: '3'
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres
View Code

# 自定义DNS服务器,可以是单个值或列表

dns

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9
View Code

# 覆盖entrypoin

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
View Code

# 从文件添加环境变量,可以是单个值或列表

env_file

env_file: .env

env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env
View Code

# 添加环境变量,可以是数组或字典。布尔值用引号括起来。

environment

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

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET
View Code

# 声明容器服务端口

expose

expose:
 - "3000"
 - "8000"
View Code

# 连接到另一个容器

links

web:
  links:
   - db
   - db:database
   - redis
View Code

# 连接Compose之外的容器

external_links

external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql
View Code

# 添加主机名映射,与—addhost相同

extra_hosts

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

# 记录该服务的日志。与—logdriver相同

logging

logging:
  driver: syslog
  options:
    syslog-address: "tcp://192.168.0.42:123"
View Code

# 网络模式,与—net相同

network_mode

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
View Code

# 要加入的网络。

networks

services:
  some-service:
    networks:
     - some-network
     - other-network
View Code

# 在加入网络时为该服务指定容器的静态IP地址

aliases

ipv4_address,ipv6_address

version: '2.1'

services:
  app:
    image: busybox
    command: ifconfig
    networks:
      app_net:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10

networks:
  app_net:
    driver: bridge
    enable_ipv6: true
    ipam:
      driver: default
      config:
      -
        subnet: 172.16.238.0/24
      -
        subnet: 2001:3984:3989::/64
View Code

# 将PID模式设置主机PID模式,与宿主机共享PID地址空间。pid: “host”

pid

pid: "host"
View Code

# 暴露端口,与-p相同。但端口不低于60

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"
View Code

# 再容器内设置内核参数,可以是数组或字典

sysctls

sysctls:
  net.core.somaxconn: 1024
  net.ipv4.tcp_syncookies: 0

sysctls:
  - net.core.somaxconn=1024
  - net.ipv4.tcp_syncookies=0
View Code

# 覆盖容器的默认ulimits

ulimits

ulimits:
  nproc: 65535
  nofile:
    soft: 20000
    hard: 40000
View Code

# 挂载一个目录或一个已存在的数据卷容器到容器

volumes

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
View Code
version: '3.3'
services:
  redis:
    image: 192.168.1.81:5000/redis:4.0.6
    volumes:
      - type: volume
        source: redis-nfs
        target: /usr/local/redis-4.0.9/data
        volume:
          nocopy: true

volumes:
  redis-nfs:
    driver: local
    driver_opts:
      type: "nfs"
      o: "addr=192.168.1.81,vers=4,soft,timeo=180,bg,tcp,rw"
      device: "192.168.1.81:/data"
创建NFS持久化

# 默 认 no , always|onfailure|unless-stopped

restart

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

# 主机名

hostname

domainname: foo.com
hostname: foo
ipc: host
mac_address: 02:42:ac:11:65:43
View Code

# 工作目录

working_dir

user: postgresql
working_dir: /code
View Code

 

更多命令:https://docs.docker.com/compose/compose-file/compose-file-v2/

posted @ 2018-10-24 11:15  kevin.Xiang  阅读(3682)  评论(1编辑  收藏  举报