將 Docker Compose 應用程式打包並部署為 OCI artifact
Docker Compose 支援使用 OCI artifact,允許您透過容器登錄檔打包和分發 Compose 應用程式。這意味著您可以將 Compose 檔案與容器映象一起儲存,從而更輕鬆地版本化、共享和部署多容器應用程式。
將 Compose 應用程式釋出為 OCI artifact
要將您的 Compose 應用程式分發為 OCI artifact,您可以使用 `docker compose publish` 命令將其釋出到符合 OCI 標準的登錄檔。這允許其他人直接從登錄檔部署您的應用程式。
釋出功能支援 Compose 的大多數組合功能,如覆蓋、擴充套件或包含,但存在一些限制。
一般步驟
導航到您的 Compose 應用程式目錄。
確保您位於包含 `compose.yml` 檔案的目錄中,或者您正在使用 `-f` 標誌指定您的 Compose 檔案。在您的終端中,登入您的 Docker 帳戶以透過 Docker Hub 進行身份驗證。
$ docker login
使用 `docker compose publish` 命令將您的應用程式作為 OCI artifact 推送
$ 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`:將映象標籤固定到摘要。
- `--with-env`:在釋出的 OCI artifact 中包含環境變數。
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 artifact 存在限制。您無法釋出以下 Compose 配置:
- 包含繫結掛載的服務
- 僅包含 `build` 部分的服務
- 包含帶有 `include` 屬性的本地檔案。要成功釋出,請確保任何包含的本地檔案也已釋出。然後您可以使用 `include` 引用這些檔案,因為支援遠端 `include`。
啟動 OCI artifact 應用程式
要啟動使用 OCI artifact 的 Docker Compose 應用程式,您可以使用 `-f`(或 `--file`)標誌,後跟 OCI artifact 引用。這允許您指定儲存在登錄檔中的 Compose 檔案作為 OCI artifact。
`oci://` 字首表示 Compose 檔案應從符合 OCI 標準的登錄檔中拉取,而不是從本地檔案系統載入。
$ docker compose -f oci://docker.io/username/my-compose-app:latest up
要執行 Compose 應用程式,請使用 `docker compose up` 命令,並將 `-f` 標誌指向您的 OCI artifact
$ docker compose -f oci://docker.io/username/my-compose-app:latest up
故障排除
當您從 OCI artifact 執行應用程式時,Compose 可能會顯示警告訊息,要求您確認以下內容,以限制執行惡意應用程式的風險
- 使用的插值變數及其值的列表
- 應用程式使用的所有環境變數的列表
- 如果您的 OCI artifact 應用程式正在使用其他遠端資源,例如透過 `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 artifact 資源的目錄
...
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