使用 Docker Compose 與 OCI artifact

要求: Docker Compose 2.34.0 及更高版本

Docker Compose 支援使用 OCI artifact,允許您透過容器 registry 打包和分發您的 Compose 應用。這意味著您可以將 Compose 檔案與容器映象一起儲存,從而更輕鬆地對多容器應用進行版本控制、共享和部署。

將 Compose 應用釋出為 OCI artifact

要將您的 Compose 應用分發為 OCI artifact,您可以使用 docker compose publish 命令將其釋出到相容 OCI 的 registry。這使得其他人可以直接從 registry 部署您的應用。

釋出功能支援 Compose 的大多數組合能力,如 overrides、extends 或 include,但有一些限制

一般步驟

  1. 導航到您的 Compose 應用目錄。
    確保您位於包含 compose.yml 檔案的目錄中,或者使用 -f 標誌指定您的 Compose 檔案。

  2. 在終端中,登入您的 Docker 賬戶,以便向 Docker Hub 進行認證。

    $ docker login
    
  3. 使用 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: 將映象標籤固定到 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]: 

如果您拒絕,釋出過程將停止,不會向 registry 傳送任何內容。

限制

將 Compose 應用釋出為 OCI artifact 存在一些限制。您無法釋出包含以下內容的 Compose 配置:

  • 包含 bind mounts 的服務
  • 僅包含 build 部分的服務
  • 包含帶有 include 屬性的本地檔案。要成功釋出,請確保任何包含的本地檔案也已釋出。然後您可以使用 include 引用這些檔案,因為支援遠端 include

啟動 OCI artifact 應用

要啟動使用 OCI artifact 的 Docker Compose 應用,可以使用 `-f`(或 `--file`)標誌,後跟 OCI artifact 引用。這允許您指定儲存在 registry 中作為 OCI artifact 的 Compose 檔案。

oci:// 字首表示 Compose 檔案應從相容 OCI 的 registry 拉取,而不是從本地檔案系統載入。

$ 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"
頁面選項