將 Docker Compose 應用程式封裝並部署為 OCI 構件
Docker Compose 支援使用 OCI 構件,讓您能夠透過容器登錄庫來封裝及散佈您的 Compose 應用程式。這表示您可以將 Compose 檔案與您的容器映像檔儲存在一起,進而更容易地進行版本管理、分享及部署多容器應用程式。
將您的 Compose 應用程式發佈為 OCI 構件
若要將您的 Compose 應用程式以 OCI 構件形式散佈,您可以使用 docker compose publish 指令將其發佈到符合 OCI 標準的登錄庫。這能讓其他人直接從登錄庫部署您的應用程式。
發佈功能支援大多數 Compose 的組合功能,例如覆蓋 (overrides)、擴充 (extends) 或包含 (include),但有部分限制。
一般步驟
導覽至您的 Compose 應用程式目錄。
確保您位於包含compose.yml檔案的目錄,或者您已使用-f旗標指定了 Compose 檔案。在終端機中登入您的 Docker 帳號,以便通過 Docker Hub 的驗證。
$ docker login使用
docker compose publish指令將您的應用程式作為 OCI 構件推送到登錄庫。$ docker compose publish username/my-compose-app:latest如果您有多個 Compose 檔案,請執行
$ docker compose -f compose-base.yml -f compose-production.yml publish username/my-compose-app:latest
進階發佈選項
發佈時,您可以傳入額外的選項
--oci-version:指定 OCI 版本(預設為自動判定)。--resolve-image-digests:將映像檔標籤鎖定 (pin) 到摘要 (digests)。--with-env:將環境變數包含在已發佈的 OCI 構件中。
Compose 會檢查設定中是否包含任何敏感資料,並顯示您的環境變數以供確認是否要發佈。
...
you are about to publish sensitive data within your OCI artifact.
please double check that you are not leaking sensitive data
AWS Client ID
"services.serviceA.environment.AWS_ACCESS_KEY_ID": xxxxxxxxxx
AWS Secret Key
"services.serviceA.environment.AWS_SECRET_ACCESS_KEY": aws"xxxx/xxxx+xxxx+"
Github authentication
"GITHUB_TOKEN": ghp_xxxxxxxxxx
JSON Web Token
"": xxxxxxx.xxxxxxxx.xxxxxxxx
Private Key
"": -----BEGIN DSA PRIVATE KEY-----
xxxxx
-----END DSA PRIVATE KEY-----
Are you ok to publish these sensitive data? [y/N]:y
you are about to publish environment variables within your OCI artifact.
please double check that you are not leaking sensitive data
Service/Config serviceA
FOO=bar
Service/Config serviceB
FOO=bar
QUIX=
BAR=baz
Are you ok to publish these environment variables? [y/N]: 如果您拒絕,發佈程序將會停止,且不會傳送任何內容到登錄庫。
限制
將 Compose 應用程式作為 OCI 構件發佈存在一些限制。您無法發佈包含下列項目的 Compose 設定:
- 包含繫結掛載 (bind mounts) 的服務。
- 僅包含
build區段的服務。 - 包含使用
include屬性的本機檔案。若要成功發佈,請確保任何包含的本機檔案也已發佈。接著,您可以使用include來參照這些檔案,因為系統支援遠端include。
啟動 OCI 構件應用程式
若要啟動使用 OCI 構件的 Docker Compose 應用程式,您可以使用 -f(或 --file)旗標,後接 OCI 構件參照。這讓您能夠指定儲存在登錄庫中的 OCI 構件作為 Compose 檔案。
oci:// 前綴表示該 Compose 檔案應從符合 OCI 標準的登錄庫中提取,而不是從本機檔案系統載入。
$ docker compose -f oci://docker.io/username/my-compose-app:latest up
若要執行該 Compose 應用程式,請使用 docker compose up 指令,並透過 -f 旗標指向您的 OCI 構件。
$ docker compose -f oci://docker.io/username/my-compose-app:latest up
疑難排解
當您執行來自 OCI 構件的應用程式時,Compose 可能會顯示警告訊息,要求您確認以下事項,以降低執行惡意應用程式的風險:
- 所使用的插值變數列表及其值。
- 應用程式所使用的所有環境變數列表。
- 如果您的 OCI 構件應用程式正在使用其他遠端資源(例如透過
include)。
$ REGISTRY=myregistry.com docker compose -f oci://docker.io/username/my-compose-app:latest up
Found the following variables in configuration:
VARIABLE VALUE SOURCE REQUIRED DEFAULT
REGISTRY myregistry.com command-line yes
TAG v1.0 environment no latest
DOCKERFILE Dockerfile default no Dockerfile
API_KEY <unset> none no
Do you want to proceed with these variables? [Y/n]:y
Warning: This Compose project includes files from remote sources:
- oci://registry.example.com/stack:latest
Remote includes could potentially be malicious. Make sure you trust the source.
Do you want to continue? [y/N]: 如果您同意啟動應用程式,Compose 會顯示從 OCI 構件下載的所有資源所在的目錄。
...
Do you want to continue? [y/N]: y
Your compose stack "oci://registry.example.com/stack:latest" is stored in "~/Library/Caches/docker-compose/964e715660d6f6c3b384e05e7338613795f7dcd3613890cfa57e3540353b9d6d"docker compose publish 指令支援非互動式執行,您可以包含 -y(或 --yes)旗標來跳過確認提示。
$ docker compose publish -y username/my-compose-app:latest