參考和指南
這些主題描述了Compose文件格式的第3版。這是最新的版本.
Compose and Docker 兼容性矩陣
有幾個版本的Compose文件格式 - 1,2,2.x和3.x.下表是快速瀏覽。有關每個版本包含和如何升級的詳細信息,請參閱關於版本和升級.
Compose 文件結構和示例
version: "3"
services:
redis:
image: redis:alpine
ports:
- "6379"
networks:
- frontend
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- 5000:80
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- 5001:80
networks:
- backend
depends_on:
- db
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
placement:
constraints: [node.role == manager]
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
frontend:
backend:
volumes:
db-data:
此參考頁面上的主題按頂級按鍵按字母順序組織,以反映撰寫文件本身的結構。定義配置文件中的一部分,如頂級鍵build,deploy,depends_on, networks,等等,都與所有支持他們的子課題的選項中列出。這將映射到
一個好的開始是入門教程,它使用版本3編寫堆棧文件來實現多容器應用程序,服務定義和群組模式。以下是本教程中使用的一些撰寫文件。,
另一個很好的參考是在
Docker for Beginners實驗室
主題 將應用程序部署到Swarm. 上的投票應用程序示例的compose文件 。這也在本節頂部上展示。
服務配置參考
Compose文件是一個定義
服務,
網絡 和
卷的YAML 文件.
Compose文件的默認路徑是 ./docker-compose.yml
.
提示:您可以對此文件使用
.yml
或.yaml
擴展名。他們都工作.
服務定義包含將應用於為該服務啟動的每個容器的配置,就像傳遞命令行參數一樣 docker run
。同樣,網絡和卷的定義類似於 docker network create
和docker volume create
。
正如docker run
在Dockerfile指定選項(例如,CMD
, EXPOSE
,VOLUME
,ENV
)是默認的尊重-你不需要再次指定它們docker-compose.yml。
您可以使用類似Bash的${VARIABLE}
語法在配置值中使用環境變量 - 有關詳細信息,請參閱
變量替換 for full details.
本節包含版本3中服務定義支持的所有配置選項的列表。
build
build
可以指定為包含構建上下文的路徑的字符串:
version: '2'
services:
webapp:
build: ./dir
或者,作為一個對象,具有 上下文 和可選的Dockerfile 和 args下指定的路徑:
version: '2'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
如果指定image
以及build
,然后撰寫的名稱與內置的圖像webapp
和可選的tag
規定image
:
build: ./dir
image: webapp:tag
這將導致命名的鏡像webapp
和標簽tag
,從建成./dir
。
注意:在使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。該docker stack
命令僅接受預建圖像。
上下文
到包含Dockerfile的目錄的路徑,或者是git倉庫的URL。
當提供的值是相對路徑時,它被解釋為相對於撰寫文件的位置。該目錄也是發送到Docker守護程序的構建上下文。
Compose將使用生成的名稱構建並標記它,然后使用該映像。
build:
context: ./dir
Dockerfile
備用Docker文件。
Compose將使用備用文件來構建。還必須指定構建路徑。
build:
context: .
dockerfile: Dockerfile-alternate
Args
添加構建參數,環境變量只能在構建過程中訪問。
首先,在Dockerfile中指定參數:
ARG buildno
ARG password
RUN echo "Build number: $buildno"
RUN script-requiring-password.sh "$password"
然后在build
密鑰下指定參數。您可以傳遞映射或列表:
build:
context: .
args:
buildno: 1
password: secret
build:
context: .
args:
- buildno=1
- password=secret
您可以在指定構建參數時省略該值,在這種情況下,構建時的值是運行Compose的環境中的值。
args:
- buildno
- password
注:YAML布爾值(
true
,false
,yes
,no
,on
,off
)必須用引號括起來,這樣分析器會將它們解釋為字符串。
cache_from
注意:此選項在v3.2中是新增功能
引擎將用於緩存解析的圖像列表。
build:
context: .
cache_from:
- alpine:latest
- corp/web_app:3.14
labels
注意:此選項在v3.3中是新增的
使用 Docker labels
將元數據添加到生成的圖像。您可以使用數組或字典。
建議您使用反向DNS符號來防止標簽與其他軟件使用的標簽相沖突。
build:
context: .
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
build:
context: .
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
cap_add, cap_drop
添加或刪除容器功能。查看man 7 capabilities
完整列表。
cap_add:
- ALL
cap_drop:
- NET_ADMIN
- SYS_ADMIN
注意:在 使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略這些選項 。
command
覆蓋默認命令。
command: bundle exec thin -p 3000
該命令也可以是一個列表,類似於
dockerfile:
command: ["bundle", "exec", "thin", "-p", "3000"]
configs
使用每個服務configs
配置在每個服務的基礎上授予對配置的訪問權限。支持兩種不同的語法變體。
注意:該配置必須已經存在或 在configs 該堆棧文件的 頂級配置
中定義,否則堆棧部署將失敗
Short syntax
短語法變體只指定配置名稱。這允許容器訪問該配置並將其安裝在/<config_name>
容器內。源名稱和目標安裝點都設置為配置名稱。
以下示例使用簡短的語法來授予redis
對my_config
和my_other_config
配置的服務訪問權限。將值 my_config
設置為文件的內容./my_config.txt
,並將 my_other_config
其定義為外部資源,這意味着它已經在Docker中定義,無論是通過運行docker config create
命令還是通過其他堆棧部署。如果外部配置不存在,則堆棧部署失敗並出現config not found
錯誤。
注意:
config
定義僅在版本3.3及更高版本的組合文件格式中受支持。
version: "3.3"
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
如果沒有指定,則默認為Linux。Windows不支持mode
:將在服務的任務容器中裝載的文件的權限,以八進制符號表示。例如,0444
代表世界可讀。默認是0444
。配置不能被寫入,因為它們被安裝在臨時文件系統中,所以如果設置可寫位,則忽略它。可執行位可以設置。如果您不熟悉UNIX文件權限模式,您可能會發現此
權限計算器 很有用.
下面的示例設置的名稱my_config
,以redis_config
在容器內,將模式設定為0440
(組可讀),並且將所述用戶和組103
。該redis
服務無法訪問該my_other_config
配置。
version: "3.3"
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
您可以授予對多個配置的服務訪問權限,您可以混合長短語法。定義配置並不意味着授予對其的訪問權限。
cgroup_parent
為容器指定一個可選的父cgroup。
cgroup_parent: m-executor-abcd
注意:在 使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
container_name
指定自定義容器名稱,而不是生成的默認名稱。
container_name: my-web-container
因為Docker容器名稱必須是唯一的,如果您指定了自定義名稱,則無法將服務擴展到超過1個容器。嘗試這樣做會導致錯誤。
注意:在 使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
credential_spec
注意:此選項已在v3.3中添加
配置托管服務帳戶的憑據規范。此選項僅用於使用Windows容器的服務。在credential_spec
必須在格式file://<filename>
或registry://<value-name>
。
在使用時file:
,引用的文件必須存在於CredentialSpecs docker
數據目錄的子目錄中,這C:\ProgramData\Docker\
在Windows上是默認的。以下示例從名為C:\ProgramData\Docker\CredentialSpecs\my-credential-spec.json
以下文件的文件加載憑據規范 :
credential_spec:
file: my-credential-spec.json
使用時registry
:,憑據規范從守護程序主機上的Windows注冊表中讀取。具有給定名稱的注冊表值必須位於:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\Containers\CredentialSpecs
以下示例從my-credential-spec
注冊表中指定的值加載憑據規范:
credential_spec:
registry: my-credential-spec
deploy
僅限版本3
指定與部署和運行相關的配置。這只能部署到時生效 swarm 與
docker stack deploy,並且被忽略docker-compose up
和docker-compose run
。
version: '3'
services:
redis:
image: redis:alpine
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
有幾個子選項可用:
endpoint_mode
為連接到群組的外部客戶端指定服務發現方法。
版本3.3 .
-
endpoint_mode: vip
- Docker為服務分配虛擬IP(VIP),作為客戶端到達網絡服務的“前端”。Docker在服務的客戶端和可用的工作者節點之間路由請求,而無需客戶端了解有多少個節點參與服務或其IP地址或端口。(這是默認值。) -
endpoint_mode: dnsrr
- DNS循環(DNSRR)服務發現不使用單個虛擬IP。Docker為服務設置DNS條目,以便服務名稱的DNS查詢返回IP地址列表,客戶端直接連接到其中一個。如果要使用自己的負載均衡器,或混合Windows和Linux應用程序,則DNS循環是非常有用的。
version: "3.3"
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:
endpoint_mode
在swarm模式CLI命令
docker service create也可用作標志 的選項。有關所有群集相關docker命令的快速列表,請參閱Swarm模式CLI命令.
labels
指定服務的標簽。這些標簽只能在服務上設置,而不能在服務的任何容器上設置。
version: "3"
services:
web:
image: web
deploy:
labels:
com.example.description: "This label will appear on the web service"
要在容器上設置標簽,請使用以下labels
鍵deploy
:
version: "3"
services:
web:
image: web
labels:
com.example.description: "This label will appear on all containers for the web service"
mode
任一global
(正好一個每群節點容器)或replicated
(一個指定的數量的容器)。默認是replicated
。
version: '3'
services:
worker:
image: dockersamples/examplevotingapp_worker
deploy:
mode: global
placement
指定布局約束
version: '3'
services:
db:
image: postgres
deploy:
placement:
constraints:
- node.role == manager
- engine.labels.operatingsystem == ubuntu 14.04
replicas
如果服務是replicated
(默認為),請指定在任何給定時間應運行的容器數。
version: '3'
services:
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 6
resources
配置資源約束。
注意:這取代了舊的資源約束選項在撰寫非群模式文件之前版本3(
cpu_shares,cpu_quota,cpuset, mem_limit,memswap_limit,mem_swappines
s如在 升級版本2.x到3.x.
這些都是一個單一的值,類似於docker service
create 對應的.
在這個通用示例中,redis
服務被限制為使用不超過50M
的內存和0.001
(0.1%)的可用處理時間(CPU),並且具有 保留20M
的內存和0.0001
CPU時間(一直可用)。
version: '3'
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
reservations:
cpus: '0.0001'
memory: 20M
下面的主題描述了為群集中的服務或容器設置資源約束的可用選項。
內存異常 (OOME)
如果您的服務或容器嘗試使用比系統可用的更多內存,則可能會遇到內存異常(OOME),並且容器或Docker守護程序可能被內核OOM殺手殺死。為了防止這種情況發生,請確保您的應用程序在具有足夠內存的主機上運行,並且了解內存不足的風險。
restart_policy
配置在退出時是否以及如何重新啟動容器。替換restart
.
condition
: 其中之一none
,on-failure
或any
(默認:)any
。delay
:在重新啟動嘗試之間等待多長時間,指定為持續時間
(默認值:0)。max_attempts
: 在放棄之前嘗試重新啟動一個容器多少次(默認:永不放棄).window
: 在決定重新啟動成功之前等待多長時間,指定為持續時間(默認:立即決定).
version: "3"
services:
redis:
image: redis:alpine
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
update_config
配置服務如何更新。用於配置滾動更新。
parallelism
:一次更新的容器數量。delay
:更新一組容器之間等待的時間。failure_action
: 如果更新失敗,該怎么辦?其中之一continue,rollback或pause
(默認:)pause。monitor
: 每個任務更新后的持續時間來監視失敗(ns|us|ms|s|m|h)
(默認為0)。max_failure_ratio
:更新期間容忍的故障率。order
: 更新期間的操作順序。其中一個stop-first
(舊任務,開始新的一個前停止),或者start-first
(新的任務首先啟動,並且正在運行的任務將簡要重疊)(默認stop-first)注:僅支持V3.4及更高版本。
注意:order僅支持v3.4及更高版本的撰寫文件格式。
version: '3.4'
services:
vote:
image: dockersamples/examplevotingapp_vote:before
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
order: stop-first
不支持 docker stack deploy
下面的子選項(支持docker compose up
和docker compose run
)是不支持的docker stack deploy
或deploy鍵的。
- build
- cgroup_parent
- container_name
- devices
- dns
- dns_search
- tmpfs
- external_links
- links
- network_mode
- security_opt
- stop_signal
- sysctls
- userns_mode
devices
設備映射列表 使用與--device
docker客戶端創建選項相同的格式。
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
注意:在使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
depends_on
服務之間的快速依賴,有兩個作用:
-
docker-compose up
將依依次順序啟動服務。在下面的例子中,db並redis會開始之前web。 -
docker-compose up SERVICE
將自動包含SERVICE的依賴關系。在下面的例子中,docker-compose up web也將創建和啟動db和redis。
簡單的例子:
version: '3'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
使用時需要注意幾件事情
depends_on
:
depends_on
在開始之前不會等待db
並且redis
“准備好”,web
直到它們被啟動為止版本3不再支持的
condition
形式depends_on
depends_on
使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
DNS
自定義DNS服務器。可以是單個值或列表。
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
注意:使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
dns_search
自定義DNS搜索域。可以是單個值或列表。
dns_search: example.com
dns_search:
- dc1.example.com
- dc2.example.com
注意:使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
tmpfs
版本2文件格式 and up.
在容器內安裝臨時文件系統。可以是單個值或列表。
tmpfs: /run
tmpfs:
- /run
- /tmp
注意:使用(版本3)Compose文件以群組模式部署堆棧
時,將忽略此選項 。
entrypoint
覆蓋默認入口點。
entrypoint: /code/entrypoint.sh
entrypoint也可以是一個列表,類似於
dockerfile:
entrypoint:
- php
- -d
- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
- -d
- memory_limit=-1
- vendor/bin/phpunit
注意:設置
entrypoint
將使用ENTRYPOINTDockerfile
指令覆蓋服務映像上的任何默認入口點集,並 清除映像上的任何默認命令 - 這意味着如果CMD
Dockerfile中有指令,則將忽略它。
env_file
從文件添加環境變量。可以是單個值或列表。
如果您指定了一個Compose文件docker-compose -f FILE,則路徑 env_file相對於該文件所在的目錄。
環境變量 部分中 聲明的環境變量將覆蓋這些值 - 即使這些值為空或未定義,這也將成立。
env_file: .env
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
Compose期望env文件中的每一行都是VAR=VAL
格式化的。以#
(即注釋)開始的行將被忽略,空白行也將被忽略。
# Set Rails/Rack environment
RACK_ENV=development
注意:如果您的服務指定構建選項,則在構建期間將不會自動顯示在環境文件中定義的變量。使用
args
子選項build
定義構建時環境變量。
該值VAL
是按原樣使用的,完全沒有修改。例如,如果值被引號包圍(通常是shell變量的情況),引號將包含在傳遞給Compose的值中。
請記住,在確定分配給顯示多次的變量的值時,列表中文件的順序很重要。列表中的文件從上到下處理。對於在文件中指定的相同變量,a.env
並在文件中 分配了不同的值b.env,如果b.env在下面列出(之后),則來自b.envstand 的值。例如,給出以下聲明docker_compose.yml
:
services:
some-service:
env_file:
- a.env
- b.env
和以下文件:
# a.env
VAR=1
和
# b.env
VAR=hello
$ VAR將會hello
environment
添加環境變量。您可以使用數組或字典。任何布爾值; true,false,yes no,需要用引號括起來,以確保它們不被YML解析器轉換為True或False。
僅具有密鑰的環境變量將被解析為其正在運行的機器上的值,這對於秘密或主機特定值有幫助。
environment:
RACK_ENV: development
SHOW: 'true'
SESSION_SECRET:
environment:
- RACK_ENV=development
- SHOW=true
- SESSION_SECRET
注意:如果您的服務指定構建選項,environment則在構建期間不會自動顯示定義的變量。使用args子選項build定義構建時環境變量。
expose
暴露端口而不將它們發布到主機 - 它們只能被鏈接服務訪問。只能指定內部端口。
expose:
- "3000"
- "8000"
external_links
連接到組合之外docker-compose.yml
或甚至外部的容器,尤其是提供共享或公共服務的容器。 在指定容器名稱和鏈接別名()時,external_links
遵循與legacy選項相似的語義。linksCONTAINER:ALIAS
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
注意:
如果您使用的是版本2或以上的文件格式,外部創建的容器必須連接至至少一個與要鏈接的服務相同的網絡。鏈接是遺留選項。我們建議使用網絡。
使用(版本3)Compose文件在群集模式下部署堆棧時,將忽略此選項。
extra_hosts
添加主機名映射。使用與docker客戶端--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
healthcheck
配置運行的支票以確定此服務的容器是否“健康”。
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
interval並被timeout指定為持續時間。
test必須是字符串或列表。如果它是一個列表,第一項必須是NONE,CMD或CMD-SHELL。如果它是一個字符串,則相當於指定CMD-SHELL該字符串。
# Hit the local web app
test: ["CMD", "curl", "-f", "http://localhost"]
# As above, but wrapped in /bin/sh. Both forms below are equivalent.
test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
test: curl -f https://localhost || exit 1
要禁用圖像設置的任何默認的健康檢查,可以使用disable: true。這相當於指定test: ["NONE"]。
healthcheck:
disable: true
image
指定要從中啟動容器的圖像。可以是存儲庫/標簽或部分映像ID。
image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd
如果圖像不存在,Compose嘗試拉它,除非您也指定了構建,在這種情況下,它使用指定的選項構建它,並使用指定的標簽進行標記。
isolation
指定容器的隔離技術。在Linux上,唯一支持的值是default。在Windows中,可接受的值是default,process和 hyperv。有關詳細信息,請參閱
Docker Engine docs
.
labels
使用Docker標簽將元數據添加到容器。您可以使用數組或字典。
建議您使用反向DNS符號來防止標簽與其他軟件使用的標簽相沖突。
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
links
鏈接到另一個服務中的容器。指定服務名稱和鏈接別名(SERVICE:ALIAS)或僅指定服務名稱。
web:
links:
- db
- db:database
- redis
鏈接服務的容器將以與別名相同的主機名或者未指定別名的服務名稱可訪問。
鏈接不需要啟用服務進行通信 - 默認情況下,任何服務都可以達到該服務名稱的任何其他服務。
鏈接還以與depends_on相同的方式表示服務之間的依賴關系 ,因此它們確定服務啟動的順序。
Notes
如果您定義了鏈接和網絡,那么它們之間鏈接的服務必須至少共享一個網絡以進行通信。
使用(版本3)Compose文件在群集模式下部署堆棧時,將忽略此選項
logging
記錄該服務的配置。
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
該driver 名稱指定服務的容器的日志記錄驅動程序,以及--log-driverdocker運行的選項(在此記錄)。
默認值為json-file。
driver: "json-file"
driver: "syslog"
driver: "none"
Note: 只有json-file和journald驅動程序可以直接從docker-compose up和docker-compose logs。使用任何其他驅動程序不會打印任何日志。
使用options鍵指定記錄驅動程序的日志記錄選項,與--log-opt選項一樣docker run。
記錄選項是鍵值對。syslog選項示例:
driver: "syslog"
options:
syslog-address: "tcp://192.168.0.42:123"
默認驅動程序json-file具有限制存儲的日志數量的選項。為此,請使用鍵值對來獲取最大存儲大小和最大文件數:
options:
max-size: "200k"
max-file: "10"
上面顯示的示例將存儲日志文件,直到它們達到max-size200kB,然后旋轉它們。存儲的各個日志文件的數量由max-file值指定。當日志超出最大限制時,會刪除較舊的日志文件以允許存儲新日志。
這是一個docker-compose.yml限制日志存儲的示例文件:
services:
some-service:
image: some-service
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
上述用於控制日志文件和大小的示例使用特定於json文件驅動程序的選項。這些特定選項在其他日志記錄驅動程序中不可用。
network_mode
網絡模式。使用與docker客戶端--net參數相同的值,加上特殊格式service:[service name]。
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
Notes
使用(版本3)Compose文件在群集模式下部署堆棧時,將忽略此選項 。
network_mode: "host"
不能與links混合.
networks
加入網絡,引用頂級networks密鑰下的條目
services:
some-service:
networks:
- some-network
- other-network
aliases
網絡上此服務的別名(替代主機名)。同一網絡上的其他容器可以使用服務名稱或此別名來連接到其中一個服務的容器。
由於aliases是網絡范圍的,相同的服務可以在不同的網絡上有不同的別名。
Note: 全網域別名可由多個容器共享,甚至可以由多個服務共享。如果是,則不能保證名稱解決的哪個容器。
一般格式如下所示。
services:
some-service:
networks:
some-network:
aliases:
- alias1
- alias3
other-network:
aliases:
- alias2
在下面的例子中,提供了三種服務(web,worker,和db),其中兩個網絡(沿new和legacy)。該db服務是在到達的主機名db或database上new網絡,並db或mysql將上legacy網絡。
version: '2'
services:
web:
build: ./web
networks:
- new
worker:
build: ./worker
networks:
- legacy
db:
image: mysql
networks:
new:
aliases:
- database
legacy:
aliases:
- mysql
networks:
new:
legacy:
ipv4_address, ipv6_address
在加入網絡時為該服務指定容器的靜態IP地址。
頂級網絡部分中的相應網絡配置 必須具有ipam覆蓋每個靜態地址的子網配置的 塊。如果需要IPv6尋址,則enable_ipv6必須設置該選項,您必須使用版本2.x Compose文件,如下所示。
一個例子:
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: "host"
將PID模式設置為主機PID模式。這將打開容器和主機操作系統之間的共享PID地址空間。使用此標志啟動的容器將能夠訪問和操作裸機機器的命名空間中的其他容器,反之亦然。
ports
Expose ports.
Short syntax
既可以指定端口(HOST:CONTAINER),也可以指定容器端口(將選擇隨機的主機端口)。
Note:以HOST:CONTAINER格式映射端口時,使用低於60的容器端口時,可能會遇到錯誤的結果,因為YAML會將格式的數字解析xx:yy為六進制(基數為60)。因此,我們建議您始終將端口映射明確指定為字符串。
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"
Long syntax
長格式語法允許配置不能以簡短形式表達的其他字段。
target
: 容器內的端口published
: 公開端港口protocol
: 端口協議(tcp或udp)mode
: host用於在每個節點上發布主機端口,或者ingress用於在負載均衡的群集模式端口上發布主機端口。
ports:
- target: 80
published: 8080
protocol: tcp
mode: host
Note: v3.2中的長語法是新的
secrets
使用每個服務secrets 配置在每個服務的基礎上授予訪問權限。支持兩種不同的語法變體。
Note: 該秘密必須已經存在或者 在secrets 該堆棧文件的頂級配置中定義,否則堆棧部署將失敗
For more information on secrets, see secrets.
Short syntax
短語法變體僅指定秘密名稱。這允許容器訪問秘密,並將其安裝在/run/secrets/<secret_name> 容器內。源名稱和目標掛載點都設置為秘密名稱。
以下示例使用簡短的語法來授予redis對該my_secret和my_other_secret機密的服務訪問權限。將值 my_secret設置為文件的內容./my_secret.txt,並將 my_other_secret其定義為外部資源,這意味着它已經在Docker中定義,無論是通過運行docker secret create 命令還是通過其他堆棧部署。如果外部機密不存在,則堆棧部署失敗並出現secret not found錯誤。
version: "3.1"
services:
redis:
image: redis:latest
deploy:
replicas: 1
secrets:
- my_secret
- my_other_secret
secrets:
my_secret:
file: ./my_secret.txt
my_other_secret:
external: true
Long syntax
The long syntax provides more granularity in how the secret is created within
the service's task containers.
source
: The name of the secret as it exists in Docker.target
: The name of the file that will be mounted in/run/secrets/
in the
service's task containers. Defaults tosource
if not specified.uid
andgid
: The numeric UID or GID which will own the file within
/run/secrets/
in the service's task containers. Both default to0
if not
specified.mode
: The permissions for the file that will be mounted in/run/secrets/
in the service's task containers, in octal notation. For instance,0444
represents world-readable. The default in Docker 1.13.1 is0000
, but will
be0444
in the future. Secrets cannot be writable because they are mounted
in a temporary filesystem, so if you set the writable bit, it is ignored. The
executable bit can be set. If you aren't familiar with UNIX file permission
modes, you may find this
permissions calculator{: target="blank" class="" }
useful.
The following example sets name of the my_secret
to redis_secret
within the
container, sets the mode to 0440
(group-readable) and sets the user and group
to 103
. The redis
service does not have access to the my_other_secret
secret.
version: "3.1"
services:
redis:
image: redis:latest
deploy:
replicas: 1
secrets:
- source: my_secret
target: redis_secret
uid: '103'
gid: '103'
mode: 0440
secrets:
my_secret:
file: ./my_secret.txt
my_other_secret:
external: true
You can grant a service access to multiple secrets and you can mix long and
short syntax. Defining a secret does not imply granting a service access to it.
security_opt
Override the default labeling scheme for each container.
security_opt:
- label:user:USER
- label:role:ROLE
Note: This option is ignored when
deploying a stack in swarm mode
with a (version 3) Compose file.
stop_grace_period
Specify how long to wait when attempting to stop a container if it doesn't
handle SIGTERM (or whatever stop signal has been specified with
stop_signal
), before sending SIGKILL. Specified
as a duration.
stop_grace_period: 1s
stop_grace_period: 1m30s
By default, stop
waits 10 seconds for the container to exit before sending
SIGKILL.
stop_signal
Sets an alternative signal to stop the container. By default stop
uses
SIGTERM. Setting an alternative signal using stop_signal
will cause
stop
to send that signal instead.
stop_signal: SIGUSR1
Note: This option is ignored when
deploying a stack in swarm mode
with a (version 3) Compose file.
sysctls
Kernel parameters to set in the container. You can use either an array or a
dictionary.
sysctls:
net.core.somaxconn: 1024
net.ipv4.tcp_syncookies: 0
sysctls:
- net.core.somaxconn=1024
- net.ipv4.tcp_syncookies=0
Note: This option is ignored when
deploying a stack in swarm mode
with a (version 3) Compose file.
ulimits
Override the default ulimits for a container. You can either specify a single
limit as an integer or soft/hard limits as a mapping.
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
userns_mode
userns_mode: "host"
Disables the user namespace for this service, if Docker daemon is configured with user namespaces.
See dockerd for
more information.
Note: This option is ignored when
deploying a stack in swarm mode
with a (version 3) Compose file.
volumes
掛載主機路徑或命名卷,指定為服務的子選項。
您可以將主機路徑作為單個服務的定義的一部分進行安裝,並且不需要在頂級volumes密鑰中定義它。
但是,如果要跨多個服務重用卷,請在頂級volumes密鑰中定義一個命名卷。使用命名卷與服務,群組和堆棧文件。
Note: 頂級 卷鍵定義一個命名卷,並從每個服務的volumes列表中引用它。這將替代volumes_from早期版本的撰寫文件格式。
該實施例顯示了一個名為體積(mydata)正在使用的web服務,和一個綁定安裝為一個單一的服務(下第一路徑定義db的服務 volumes)。該db服務還使用一個名為dbdata(稱為db服務的第二個路徑volumes)的命名卷,但使用舊的字符串格式來定義它,用於安裝一個命名卷。命名卷必須列在頂級 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
db:
image: postgres:latest
volumes:
- "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
- "dbdata:/var/lib/postgresql/data"
volumes:
mydata:
dbdata:
Short syntax
可選地指定主機(HOST:CONTAINER)或訪問模式(HOST:CONTAINER:ro)上的路徑。
您可以在主機上安裝相對路徑,該路徑將相對於正在使用的Compose配置文件的目錄進行擴展。相對路徑應該始於.或..。
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Specify an absolute path mapping
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file
- ./cache:/tmp/cache
# User-relative path
- ~/configs:/etc/configs/:ro
# Named volume
- datavolume:/var/lib/mysql
Long syntax
長格式語法允許配置不能以簡短形式表達的其他字段。
type
: 安裝類型volume,bind或tmpfssource
: 安裝的源,主機上的綁定安裝路徑,或頂級volumes密鑰中定義的卷的名稱 。不適用於tmpfs mount。target
: 容器中將要裝入卷的路徑read_only
:將卷設置為只讀的標志bind
: 配置其他綁定選項propagation
:用於綁定的傳播模式
volume
:配置其他卷選項nocopy
: 在創建卷時禁止從容器復制數據的標志
version: "3.2"
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
networks:
webnet:
volumes:
mydata:
Note: v3.2中的長語法是新的
Volumes for services, swarms, and stack files
當使用服務,群集和docker-stack.yml文件時,請記住,支持服務的任務(容器)可以部署在群集中的任何節點上,每當節點更新時,它們可能是不同的節點。
在沒有指定源的命名卷的情況下,Docker為支持服務的每個任務創建一個匿名卷。刪除關聯的容器后,匿名卷不會持久。
如果要使數據持久存在,請使用多主機感知的命名卷和卷驅動程序,以便可以從任何節點訪問數據。或者,對服務設置約束,使其任務部署在存在卷的節點上。
作為示例,Docker Labs中docker-stack.yml的表決應用程序示例的文件 定義了一個名為db運行postgres數據庫的服務。它被配置為命名卷,以便將數據保留在群集中, 並且被限制為僅在manager節點上運行。這是從該文件的相關剪輯:
version: "3"
services:
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
restart
no是默認的重新啟動策略,它不會在任何情況下重新啟動容器。當always指定時,容器總是重新啟動。on-failure如果退出代碼指示故障錯誤,該 策略將重新啟動容器。
restart: "no"
restart: always
restart: on-failure
restart: unless-stopped
domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, working_dir
這些都是一個單一的值,類似於其
docker run 對應物
user: postgresql
working_dir: /code
domainname: foo.com
hostname: foo
ipc: host
mac_address: 02:42:ac:11:65:43
privileged: true
read_only: true
shm_size: 64M
stdin_open: true
tty: true
Specifying durations
一些配置選項,如interval和timeout子選項 check,接受一個持續時間為看起來像這樣的格式的字符串:
2.5s
10s
1m30s
2h32m
5h34m56s
The supported units are us
, ms
, s
, m
and h
.
Volume configuration reference
雖然可以在文件中聲明卷作為服務聲明的一部分,但本節允許您創建volumes_from可以跨多個服務重復使用的命名卷(不依賴),並且可以使用docker命令行輕松地檢索和檢查API。有關更多信息,請參閱 docker volume子命令文檔。
以下是一個雙服務設置的示例,其中將數據庫的數據目錄與其他服務共享為卷,以便可以定期備份數據庫的數據目錄:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
backup:
image: backup-service
volumes:
- data-volume:/var/lib/backup/data
volumes:
data-volume:
頂級volumes密鑰下的條目可以為空,在這種情況下,它將使用引擎配置的默認驅動程序(在大多數情況下,這是 local驅動程序)。
driver
指定該卷使用哪個卷驅動程序。默認為Docker Engine配置為使用的任何驅動程序,這在大多數情況下是這樣 local。如果驅動程序不可用,引擎將在docker-compose up嘗試創建卷時返回錯誤 。
driver: foobar
driver_opts
指定選項列表作為鍵值對,以傳遞給此卷的驅動程序。這些選項與驅動程序相關 - 有關詳細信息,請參閱驅動程序文檔。可選的。
driver_opts:
foo: "bar"
baz: 1
external
如果設置為true,則指定此卷已在Compose之外創建。docker-compose up不會嘗試創建它,如果不存在則會引發錯誤。
external不能與其他卷配置鍵(driver,driver_opts)結合使用。
在下面的示例中,[projectname]_dataCompose 不是嘗試創建一個名為的卷,而是 會查找一個簡單調用的現有卷,data並將其安裝到db服務的容器中。
version: '2'
services:
db:
image: postgres
volumes:
- data:/var/lib/postgresql/data
volumes:
data:
external: true
您還可以在Compose文件中與用於引用卷的名稱分開指定卷的名稱:
volumes:
data:
external:
name: actual-name-of-volume
總是使用docker stack deploy創建外部卷
如果使用docker stack deploy以swarm模式啟動應用程序 (而不是docker組合),則將創建不存在的外部卷。在群集模式下,當由服務定義時,會自動創建一個卷。由於服務任務在新節點上安排, 所以swarmkit會在本地節點上創建卷。
labels
使用Docker標簽將元數據添加到容器 。您可以使用數組或字典。
建議您使用反向DNS符號來防止標簽與其他軟件使用的標簽相沖突。
labels:
com.example.description: "Database volume"
com.example.department: "IT/Ops"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Database volume"
- "com.example.department=IT/Ops"
- "com.example.label-with-empty-value"
Network configuration reference
頂級networks密鑰允許您指定要創建的網絡。
-
For a full explanation of Compose's use of Docker networking features and all
network driver options, see the Networking guide. -
For Docker Labs
tutorials on networking, start with Designing Scalable, Portable Docker
Container
Networks
driver
指定該網絡應使用哪個驅動程序。
默認驅動程序取決於您使用的Docker Engine是如何配置的,但在大多數情況下,它將bridge位於單個主機和overlaySwarm上。
如果驅動程序不可用,Docker Engine將返回一個錯誤。
driver: overlay
bridge
Docker默認bridge在單個主機上使用網絡。
overlay
該overlay驅動程序創建一個跨多個節點命名的網絡
swarm.
-
For a working example of how to build and use an
overlay
network with a service in swarm mode, see the Docker Labs tutorial on
Overlay networking and service
discovery. -
For an in-depth look at how it works under the hood, see the
networking concepts lab on the Overlay Driver Network
Architecture.
host or none
使用主機的網絡堆棧,或沒有網絡。等同於 docker run --net=host或docker run --net=none。僅在使用docker stack命令時使用 。如果使用該docker-compose命令,請改用network_mode。
使用內置的網絡,如語法host和none稍有不同。使用名稱host或none(Docker已經自動創建的)和Compose可以使用的別名(hostnet或nonet在這些示例中)定義外部網絡,然后使用別名授予對該網絡的服務訪問權限。
services:
web:
...
networks:
hostnet: {}
networks:
hostnet:
external:
name: host
services:
web:
...
networks:
nonet: {}
networks:
nonet:
external:
name: none
driver_opts
指定選項列表作為鍵值對,以傳遞給此網絡的驅動程序。這些選項與驅動程序相關 - 有關詳細信息,請參閱驅動程序文檔。可選的。
driver_opts:
foo: "bar"
baz: 1
attachable
Note: Only supported for v3.2 and higher.
僅在driver設置時使用overlay。如果設置為true,則除了服務之外,獨立容器可以附加到此網絡。如果獨立的容器附加到覆蓋網絡,則它可以與服務和獨立容器通信,這些容器也從其他Docker守護程序連接到覆蓋網絡。
networks:
mynet1:
driver: overlay
attachable: true
enable_ipv6
在此網絡上啟用IPv6網絡。
Compose File版本3中不支持
enable_ipv6
要求您使用版本2 Compose文件,因為在Swarm模式下此命令尚不支持。
ipam
Specify custom IPAM config. This is an object with several properties, each of
which is optional:
driver
: Custom IPAM driver, instead of the default.config
: A list with zero or more config blocks, each containing any of
the following keys:subnet
: Subnet in CIDR format that represents a network segment
A full example:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
Note: Additional IPAM configurations, such as
gateway
, are only honored for version 2 at the moment.
internal
By default, Docker also connects a bridge network to it to provide external
connectivity. If you want to create an externally isolated overlay network,
you can set this option to true
.
labels
Add metadata to containers using
Docker labels. You can use either
an array or a dictionary.
It's recommended that you use reverse-DNS notation to prevent your labels from
conflicting with those used by other software.
labels:
com.example.description: "Financial transaction network"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Financial transaction network"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
external
If set to true
, specifies that this network has been created outside of
Compose. docker-compose up
will not attempt to create it, and will raise
an error if it doesn't exist.
external
cannot be used in conjunction with other network configuration keys
(driver
, driver_opts
, ipam
, internal
).
In the example below, proxy
is the gateway to the outside world. Instead of
attempting to create a network called [projectname]_outside
, Compose will
look for an existing network simply called outside
and connect the proxy
service's containers to it.
version: '2'
services:
proxy:
build: ./proxy
networks:
- outside
- default
app:
build: ./app
networks:
- default
networks:
outside:
external: true
You can also specify the name of the network separately from the name used to
refer to it within the Compose file:
networks:
outside:
external:
name: actual-name-of-network
configs configuration reference
The top-level configs
declaration defines or references
configs which can be granted to the services in this
stack. The source of the config is either file
or external
.
file
: The config is created with the contents of the file at the specified
path.external
: If set to true, specifies that this config has already been
created. Docker will not attempt to create it, and if it does not exist, a
config not found
error occurs.
In this example, my_first_config
will be created (as
<stack_name>_my_first_config)
when the stack is deployed,
and my_second_config
already exists in Docker.
configs:
my_first_config:
file: ./config_data
my_second_config:
external: true
Another variant for external configs is when the name of the config in Docker
is different from the name that will exist within the service. The following
example modifies the previous one to use the external config called
redis_config
.
configs:
my_first_config:
file: ./config_data
my_second_config:
external:
name: redis_config
You still need to grant access to the config to each service in the
stack.
secrets configuration reference
The top-level secrets
declaration defines or references
secrets which can be granted to the services in this
stack. The source of the secret is either file
or external
.
file
: The secret is created with the contents of the file at the specified
path.external
: If set to true, specifies that this secret has already been
created. Docker will not attempt to create it, and if it does not exist, a
secret not found
error occurs.
In this example, my_first_secret
will be created (as
<stack_name>_my_first_secret)
when the stack is deployed,
and my_second_secret
already exists in Docker.
secrets:
my_first_secret:
file: ./secret_data
my_second_secret:
external: true
Another variant for external secrets is when the name of the secret in Docker
is different from the name that will exist within the service. The following
example modifies the previous one to use the external secret called
redis_secret
.
secrets:
my_first_secret:
file: ./secret_data
my_second_secret:
external:
name: redis_secret
You still need to grant access to the secrets to each service in the
stack.
Variable substitution
{% include content/compose-var-sub.md %}