Docker Compose 配置文件經常使用指令

Docker Compose 配置文件經常使用指令php


YAML文件格式及編寫注意事項mysql

  • YAML是一種標記語言很直觀的數據序列化格式,可讀性高。相似於XML數據描述語言,語法比XML簡單的不少。
  • YAML數據結構經過縮進來表示,連續的項目經過減號來表示,鍵值對用冒號分隔,數組用中括號括起來,hash用花括號括起來。

YAML文件格式注意事項:nginx

  • 1. 不支持製表符tab鍵縮進,須要使用空格縮進
  • 2. 一般開頭縮進2個空格
  • 3. 字符後縮進1個空格,

# 構建鏡像上下文路徑git

buildweb

build: ./dir

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

# 指定Dockefile文件名redis

dockerfilesql

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

# 來自鏡像docker

imageubuntu

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/

相關文章
相關標籤/搜索