compose文件格式
定義配置文件中的部分(如構建,部署,依賴,網絡等)的頂級鍵與支持它們作爲子主題的選項一起列出。這映射到Compose文件的<key>:<option>:<value>
縮進結構
服務配置參考
Compose文件是定義services,networks和volumes的YAML文件。 Compose文件的默認路徑是./docker-compose.yml。
服務定義包含應用於爲該服務啓動的每個容器的配置. 類似:
docker container create
docker network create
docker volume create
build
- 在構建時應用的配置選項. 可以指定爲包含構建上下文路徑的字符串.
version: "3.7" #版本
services: #服務
nginx: #服務名稱
build: ./dir #指定構建目錄
image: nginx:tag #指定基礎鏡像
context
- 包含Dockerfile的目錄的路徑,或者是git存儲庫的url。
- 當提供的值是相對路徑時,它被解釋爲相對於Compose文件的位置。此目錄也是發送到Docker守護程序的構建上下文。
build:
context: ./dir
dockerfile
- Compose使用備用文件來構建。還必須指定構建路徑。
build:
context: .
dockerfile: Dockerfile-alternate
ARGS
- 添加構建參數.在dockerfile中定義:
ARG buildno
ARG gitcommithash
- 指定參數在build指令下構建
build:
context: .
args:
buildno: 1
注意: 在Dockerfile中,如果在FROM指令之前指定ARG,則在FROM下的構建指令中不能使用ARG。
YAML布爾值(true,false,yes,no,on,off)必須用引號括起來,以便解析器將它們解釋爲字符串。
CACHE_FROM
build:
context: .
cache_from:
- alpine:latest
- corp/web_app:3.14
LABELS
- 使用Docker標籤將元數據添加到生成的鏡像中. 可以使用數組或字典。
build:
context: .
labels:
com.example.description: "Accounting webapp"
SHM_SIZE
- 爲此構建的容器設置
/dev/shm
分區的大小
build:
context: .
shm_size: '2gb'
TARGET
- 在內部定義構建指定的階段dockerfile
- 多階段構建文檔鏈接: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
build:
context: .
target: prod
cap_add和cap_drop
- 添加或刪除容器功能
cap_add:
- ALL
cap_drop:
- NET_ADMIN
- SYS_ADMIN
cgroup_parent
- 爲容器指定可選的父cgroup
cgroup_parent: m-executor-abcd
command
- 覆蓋默認命令,和dockerfile指令相似.
command: ["bundle", "exec", "thin", "-p", "3000"]
configs
- 短語法變體僅指定配置名稱。這將授予容器對配置的訪問權限,並將其安裝在容器中的
/ <config_name>
中。源名稱和目標安裝點都設置爲配置名稱. 支持3.3版本以上. - 以下示例使用短語法授予對my_config和my_other_config配置的redis服務訪問權限。 my_config的值設置爲./my_config.txt文件的內容,my_other_config被定義爲外部資源,這意味着它已經在Docker中定義,通過運行docker config create命令或另一個堆棧部署。如果外部配置不存在,則堆棧部署將失敗並顯示config not found錯誤
version: "3.7"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- my_config
- my_other_config
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
LONG SYNTAX
source
: Docker中存在的配置名稱target
: 要在服務的任務容器中裝入的文件的路徑和名稱。默認爲:/<source>
uid和gid
: 在服務的任務容器中擁有已裝入的配置文件的數字UID或GID.默認爲0.mode
: 以八進制表示法在服務的任務容器中裝入的文件的權限。默認值爲0444.配置無法寫入,因爲它們安裝在臨時文件系統中,因此如果設置了可寫位,則會將其忽略。可以設置可執行位。- 以下示例將my_config的名稱設置爲容器中的redis_config,將模式設置爲0440(組可讀)並將用戶和組設置爲103. redis服務無權訪問my_other_config配置。
version: "3.7"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- source: my_config
target: /redis_config
uid: '103'
gid: '103'
mode: 0440
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
- 可以授予對多個配置的服務訪問權限.
container_name
- 指定自定義容器的名稱.
container_name: nginx-test
- Docker容器名稱必須是唯一的,如果指定了自定義名稱,則無法將服務擴展到多個容器.
EXAMPLE GMSA CONFIGURATION
- 爲服務配置gMSA憑據規範時,只需指定憑據規範.
version: "3.8"
services:
myservice:
image: myimage:latest
credential_spec:
config: my_credential_spec
configs:
my_credentials_spec:
file: ./my-credential-spec.json|
depends_on
- 服務依賴關係之間的Express依賴關係會導致以下行爲:
docker-compose up
: 以依賴順序啓動服務.docker-compose up SERVICE
: 自動包含SERVICE的依賴項.docker-compose stop
: 按依賴順序停止服務.
version: "3.7"
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
deploy
- 指定與部署和運行服務相關的配置.
version: "3.7"
services:
redis:
image: redis:alpine
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
ENDPOINT_MODE
- 爲連接到羣集的外部客戶端指定服務發現方法。
endpoint_mode: vip
: Docker爲服務分配虛擬IP(VIP),作爲客戶端到達網絡服務的前端。 Docker在客戶端和服務的可用工作節點之間路由請求,而無需客戶端知道有多少節點參與服務或其IP地址或端口。endpoint_mode: dnsrr
: DNS循環(DNSRR)服務發現不使用單個虛擬IP。 Docker爲服務設置DNS條目,以便服務名稱的DNS查詢返回IP地址列表,客戶端直接連接到其中一個。
version: "3.7"
services:
wordpress:
image: wordpress
ports:
- "8080:80"
networks:
- overlay
deploy:
mode: replicated
replicas: 2
endpoint_mode: vip
mysql:
image: mysql
volumes:
- db-data:/var/lib/mysql/data
networks:
- overlay
deploy:
mode: replicated
replicas: 2
endpoint_mode: dnsrr
volumes:
db-data:
networks:
overlay:
LABELS
- 指定服務的標籤。這些標籤僅在服務上設置,而不在服務的任何容器上設置。
version: "3.7"
services:
web:
image: web
deploy:
labels:
com.example.description: "This label will appear on the web service"
- 要在容器上設置標籤,可以把labels鍵放在和image鍵同級.
MODE
- 全局或複製(副本容器)
version: "3.7"
services:
worker:
image: dockersamples/examplevotingapp_worker
deploy:
mode: global
PLACEMENT
- 指定約束和首選項的位置
version: "3.7"
services:
db:
image: postgres
deploy:
placement:
constraints:
- node.role == manager
- engine.labels.operatingsystem == ubuntu 14.04
preferences:
- spread: node.labels.zone
REPLICAS
- 如果複製了服務,請指定在任何給定時間應運行的容器數。
version: "3.7"
services:
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 6
RESOURCES
- 配置資源限制.
- 在這個示例中,redis服務被限制爲使用不超過50M的內存和0.50(單核的50%)的可用處理時間(CPU),並且具有20M的內存和0.25個CPU時間保留(始終可用).
version: "3.7"
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M
Out Of Memory Exceptions (OOME)
- 如果您的服務或容器嘗試使用的內存超過系統可用的內存,則可能會遇到內存不足異常(OOME),並且內核OOM殺手可能會殺死容器或Docker守護程序。要防止這種情況發生,請確保您的應用程序在具有足夠內存的主機上運行.
RESTART_POLICY
- 配置是否以及如何在容器退出時重新啓動容器.
- condition:
none
,on-failure
,any - delay: 重啓嘗試之間等待多長時間,默認0.
- max_attempts: 在放棄之前嘗試重啓容器的次數.例如,如果max_attempts設置爲“2”,並且第一次嘗試時重新啓動失敗,則可能會嘗試重新啓動兩次以上。
- window: 在決定重啓是否成功之前等待多長時間.
- condition:
version: "3.7"
services:
redis:
image: redis:alpine
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
ROLLBACK_CONFIG
- 配置在更新失敗的情況下應如何回滾服務.
- `parallelism: 一次回滾的容器數. 0爲同時回滾.
- delay: 每個容器組的回滾之間等待的時間.
- failure_action: 如果回滾失敗該怎麼辦。選擇
continue或者pause
- monitor: 每次更新任務後的持續時間.
ns|us|ms|s|m|h
- max_failure_ratio: 回滾期間容忍的失敗率.
- order: 回滾期間的操作順序。停止優先(舊任務在啓動新任務之前停止)或啓動優先(新任務首先啓動,運行任務暫時重疊)(默認停止優先)
UPDATE_CONFIG
- 配置服務應如何更新。用於配置滾動更新.
parallelism
:一次更新的容器數delay
: 更新一組容器之間的等待時間.- failure_action: 如果更新失敗該怎麼辦。選擇
continue,rollback,pause
.默認pause. - monitor: 每次更新任務後的持續時間.
(ns|us|ms|s|m|h)
- max_failure_ratio: 更新期間容忍的失敗率。
- order : 更新期間的操作順序。 stop-first之一(舊任務在啓動新任務之前停止)或start-first(首先啓動新任務,並且運行任務暫時重疊)(默認stop-first)
version: "3.7"
services:
vote:
image: dockersamples/examplevotingapp_vote:before
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
order: stop-first
dns
- 自定義dns服務
dns:
- 8.8.8.8
- 9.9.9.9
dns_search
- 自定義DNS搜索域
dns_search:
- dc1.example.com
- dc2.example.com
entrypoint
- 覆蓋默認entrypoint
entrypoint: /code/entrypoint.sh
env_file
- 從文件添加環境變量
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
- Compose期望env文件中的每一行都是
VAR = VAL
格式。以#開頭的行被視爲註釋並被忽略。空行也被忽略。
environment
- 添加環境變量。您可以使用數組或字典。任何布爾值; true,false,yes no,需要用引號括起來
environment:
RACK_ENV: development
SHOW: 'true'
SESSION_SECRET:
expose
- 暴露端口而不將它們發佈到主機 - 它們只能被鏈接服務訪問。只能指定內部端口
expose:
- "3000"
- "8000"
external_links
- 鏈接到此docker-compose.yml之外或甚至在Compose之外的容器。指定容器名稱和鏈接別名.
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
extra_hosts
- 添加主機名映射
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
healthcheck
- 配置運行的檢查以確定此服務的容器是否“健康”.
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
test: ["CMD", "curl", "-f", "http://localhost"]
test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
test: curl -f https://localhost || exit 1
禁用運行狀況檢查
healthcheck:
disable: true
init
- 在容器內運行init,轉發信號並重新獲得進程.
version: "3.7"
services:
web:
image: alpine:latest
init: true
links
- 鏈接到另一個服務中的容器。指定服務名稱和鏈接別名.
web:
links:
- db
- db:database
- redis
- 鏈接服務的容器可以在與別名相同的主機名上訪問,如果未指定別名,則可以訪問服務名稱。
- 鏈接還以與depends_on相同的方式表達服務之間的依賴關係.