Docker Compose 配置文件經常使用指令php
YAML文件格式及編寫注意事項mysql
YAML文件格式注意事項:nginx
# 構建鏡像上下文路徑git
buildweb
build: ./dir build: context: ./dir dockerfile: Dockerfile-alternate args: buildno: 1
build: ./dir
image: webapp:tag
# 指定Dockefile文件名redis
dockerfilesql
build:
context: .
dockerfile: Dockerfile-alternate
# 來自鏡像docker
imageubuntu
image: redis image: ubuntu:14.04 image: tutum/influxdb image: example-registry.com:4000/postgresql image: a4bc65fd
# 構建參數。在Dockerfile中指定的參數 數組
args
build: context: . args: buildno: 1 gitcommithash: cdc3b19 build: context: . args: - buildno=1 - gitcommithash=cdc3b19
# 覆蓋默認命令
command
command: bundle exec thin -p 3000
command: ["bundle", "exec", "thin", "-p", "3000"]
# 自定義容器名稱。若是自定義名稱,則沒法將服務scale到1容器以外
container_name
container_name: my-web-container
# 指定與部署和運行相關的配置。限版本3
deploy
version: '3' services: redis: image: redis:alpine deploy: replicas: 6 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure
# 服務之間的依賴,控制服務啓動順序。正常是按順序啓動服務
depends_on
version: '3' services: web: build: . depends_on: - db - redis redis: image: redis db: image: postgres
# 自定義DNS服務器,能夠是單個值或列表
dns
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
# 覆蓋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
# 從文件添加環境變量,能夠是單個值或列表
env_file
env_file: .env env_file: - ./common.env - ./apps/web.env - /opt/secrets.env
# 添加環境變量,能夠是數組或字典。布爾值用引號括起來。
environment
environment: RACK_ENV: development SHOW: 'true' SESSION_SECRET: environment: - RACK_ENV=development - SHOW=true - SESSION_SECRET
# 聲明容器服務端口
expose
expose: - "3000" - "8000"
# 鏈接到另外一個容器
links
web: links: - db - db:database - redis
# 鏈接Compose以外的容器
external_links
external_links: - redis_1 - project_db_1:mysql - project_db_1:postgresql
# 添加主機名映射,與—addhost相同
extra_hosts
extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"
# 記錄該服務的日誌。與—logdriver相同
logging
logging: driver: syslog options: syslog-address: "tcp://192.168.0.42:123"
# 網絡模式,與—net相同
network_mode
network_mode: "bridge" network_mode: "host" network_mode: "none" network_mode: "service:[service name]" network_mode: "container:[container name/id]"
# 要加入的網絡。
networks
services: some-service: networks: - some-network - other-network
# 在加入網絡時爲該服務指定容器的靜態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
# 將PID模式設置主機PID模式,與宿主機共享PID地址空間。pid: 「host」
pid
pid: "host"
# 暴露端口,與-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"
# 再容器內設置內核參數,能夠是數組或字典
sysctls
sysctls: net.core.somaxconn: 1024 net.ipv4.tcp_syncookies: 0 sysctls: - net.core.somaxconn=1024 - net.ipv4.tcp_syncookies=0
# 覆蓋容器的默認ulimits
ulimits
ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000
# 掛載一個目錄或一個已存在的數據卷容器到容器
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
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"
# 默 認 no , always|onfailure|unless-stopped
restart
restart: "no" restart: always restart: on-failure restart: unless-stopped
# 主機名
hostname
domainname: foo.com
hostname: foo
ipc: host
mac_address: 02:42:ac:11:65:43
# 工做目錄
working_dir
user: postgresql
working_dir: /code
更多命令:https://docs.docker.com/compose/compose-file/compose-file-v2/