docker-compose 等同于 Docker Build --secret 参数

docker-compose Equivalent to Docker Build --secret Argument

我们使用技术 detailed here 以安全的方式将主机环境变量公开给 Docker 构建。

# syntax=docker/dockerfile:1.2
FROM golang:1.18 AS builder

# move secrets out of the build process (and docker history)
RUN --mount=type=secret,id=github_token,dst=/app/secret_github_token,required=true,uid=10001 \
    export GITHUB_TOKEN=$(cat /app/secret_github_token) && \
    <nice command that uses $GITHUB_TOKEN>

构建镜像的命令:

export DOCKER_BUILDKIT=1
docker build --secret id=github_token,env=GITHUB_TOKEN -t cool-image-bro .

以上完美运行。

现在CI中还有一个docker-compose文件运行需要修改。但是,即使我确认该作业中存在 ENV 变量,我也不知道如何将环境变量分配给 github_token 命名的秘密 ID。

换句话说,可以接受具有秘密 ID 的环境变量映射的等效 docker-compose 命令(up --build 或 build)是什么?

原来我有点超前了。 docker compose v.2.5.0 带来 support for secrets.

按上述说明修改 Docker 文件后,我们必须将 docker-compose 更新为定义的 secrets

docker-compose.yml

services:
  my-cool-app:
    build:
      context: .
      secrets:
        - github_user
        - github_token
...
secrets:
  github_user:
    file: secrets_github_user
  github_token:
    file: secrets_github_token

但是那些文件 secrets_github_usersecrets_github_token 来自哪里?在您的 CI 中,您还需要导出环境变量并将其保存到默认的机密文件位置。在我们的项目中,我们是 using Tasks,所以我们也添加了这些行。

请注意,我们正在从我们的 CI 中 运行 执行此任务,因此您可以在没有任务的情况下以不同的方式进行操作。

- printenv GITHUB_USER > /root/project/secrets_github_user
- printenv GITHUB_TOKEN > /root/project/secrets_github_token

然后我们更新 CircleCI 配置并向我们的作业添加两个环境变量:

.config.yml

  name-of-our-job:
    environment:
      DOCKER_BUILDKIT: 1
      COMPOSE_DOCKER_CLI_BUILD: 1

您可能还需要更新的 Docker 版本,我想他们是在 19 年底或 20 年初推出的。我用过它并且有效:

    steps:
      - setup_remote_docker:
          version: 20.10.11

现在,当 运行ning 你的基于 docker-compose 的命令时,秘密应该通过 docker-compose 成功安装并可用于正确构建或 运行 你的 Docker文件说明!