使用 Docker Compose 与 OCI artifact
Docker Compose 支持使用 OCI artifact,允许您通过容器 registry 打包和分发您的 Compose 应用。这意味着您可以将 Compose 文件与容器镜像一起存储,从而更轻松地对多容器应用进行版本控制、共享和部署。
将 Compose 应用发布为 OCI artifact
要将您的 Compose 应用分发为 OCI artifact,您可以使用 docker compose publish
命令将其发布到兼容 OCI 的 registry。这使得其他人可以直接从 registry 部署您的应用。
发布功能支持 Compose 的大多数组合能力,如 overrides、extends 或 include,但有一些限制。
一般步骤
导航到您的 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
: 将镜像标签固定到 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"