【问题标题】:Passing files from Google Cloud Container Builder to Docker build task将文件从 Google Cloud Container Builder 传递到 Docker 构建任务
【发布时间】:2018-01-16 04:51:06
【问题描述】:

上下文

使用 Container Builder 构建的 Ruby on Rails 应用程序,用于 App Engine。我们要求捆绑程序能够使用 SSH 密钥从私有 git 存储库安装依赖项。

SSH 密钥来自安全存储桶,通过 KMS 进行解密。这些步骤很好。但是,使用 Docker 构建容器的最后一步无法访问 SSH 密钥。

我之前没有任何丰富的 Docker 经验,所以我认为这是一个简单的问题。

cloudbuild.yml

steps:
  # Get and prepare Deploy Key
- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', 'gs://[PROJECT-BUCKET]/git_id_rsa.enc', '/root/.ssh/git_id_rsa.enc']
  volumes:
  - name: 'ssh-setup'
    path: /root/.ssh
- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - kms
  - decrypt
  - --ciphertext-file=/root/.ssh/git_id_rsa.enc
  - --plaintext-file=/root/.ssh/git_id_rsa
  - --location=global
  - --keyring=[KEYRING]
  - --key=[KEY]
  volumes:
  - name: 'ssh-setup'
    path: /root/.ssh
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: /workspace/deploy/git-prepare.sh
  volumes:
  - name: 'ssh-setup'
    path: /root/.ssh
  # ... Omitted steps ...
  # Docker build
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/[PROJECT-NAME]', '.']
  volumes:
  - name: 'ssh-setup'
    path: /root/.ssh
images: ['gcr.io/$PROJECT_ID/[PROJECT-NAME]']

省略了一些标识符

部署/git-prepare.sh — 这会执行一些 shell 命令来让 ssh 目录重新填充必要的信息。

#!/bin/bash

mkdir -p /root/.ssh

touch /root/.ssh/config
ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts

touch /root/.ssh/config
echo -e "\nHost bitbucket.org\n    IdentityFile /root/.ssh/git_id_rsa" >> /root/.ssh/config

if [ -f /root/.ssh/git_id_rsa.enc ]; then
    rm /root/.ssh/git_id_rsa.enc
fi

Dockerfile

# .. OMITTING BOILERPLATE FOR RAILS APP SETUP ...
# Copy the application files.
COPY . /app/

# Copy the SSH keys and config for bundler
VOLUME /root/.ssh

COPY /root/.ssh/known_hosts ~/.ssh/known_hosts
COPY /root/.ssh/config ~/.ssh/config
COPY /root/.ssh/git_id_rsa ~/.ssh/git_id_rsa

问题:

构建任务(使用构建触发器运行)失败:

...omitted lines above...   
Step #5: COPY failed: stat /var/lib/docker/tmp/docker-builderxxxxxxxxx/root/.ssh/known_hosts: no such file or directory
Step #5: Step 7/14 : COPY /root/.ssh/known_hosts ~/.ssh/known_hosts

我感觉我没有掌握 Container Builder 和 Docker 共享卷和数据的方式。

【问题讨论】:

    标签: docker google-container-builder


    【解决方案1】:

    Dockerfile COPY 可以使用多个<src> 资源,但文件和目录的路径将被解释为相对于构建上下文的源

    这是您执行docker build . 命令的当前路径。

    在您的情况下,如果 /root/.ssh 在 Dockerfile 执行其步骤时挂载,一个简单的 RUN cp /root/.ssh/... /destination/path 就足够了。

    但是,您无法在docker build 时间挂载卷(请参阅moby issue 14080),因此请检查this solution: a multi-stage build can help

    【讨论】:

    • 太棒了,正是我所缺乏的细节。我错误地假设 Container Builder 中的“卷”会流向 docker build 任务,但事实并非如此。我调整了我的方法,将秘密移动到. 中的一个临时目录中,Dockerfile 会根据需要对其进行整理和处理。干杯!
    【解决方案2】:

    好的,我设法做了上面答案和 cmets 中提到的事情。这就是我所做的。请注意,正如问题作者发布的那样,我在卷 /root/.ssh 中有我的 id_rsa 和 known_hosts 文件。我假设他通过这篇文章达到了他的状态:https://cloud.google.com/container-builder/docs/access-private-github-repos

    在我的 cloudbuild.yaml 中: 克隆我的 repo 之后,但在 docker build 之前,我添加了这一步:

    - name: 'gcr.io/cloud-builders/git' entrypoint: 'bash' args: - '-c' - cp /root/.ssh/{id_rsa,known_hosts} . volumes: - name: 'ssh' path: /root/.ssh

    然后,在 Dockerfile 中:

    COPY id_rsa /root/.ssh/id_rsa COPY known_hosts /root/.ssh/known_hosts RUN eval $(ssh-agent) && \ echo -e "StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \ ssh-add /root/.ssh/id_rsa

    请注意,我并不担心容器中的密钥,因为我使用的是多阶段构建。

    【讨论】:

    • 我的回答很好。 +1
    • 关于 ssh 密钥;我在 Dockerfile 中所做的是在完成构建之前删除密钥。因为 ssh 密钥只在docker build 过程中需要,而不是容器执行时需要,所以应该删除它(即使它是只读访问密钥)。无论是rm Dockerfile 中 RUN 中的文件还是cloudbuild.yaml 步骤中的文件,都取决于个人喜好。
    【解决方案3】:

    我也不得不处理同样的问题,我没有将 ssh 密钥复制到 Docker 文件中并进行清理,而是将存储库克隆到工作区作为云构建步骤。工作空间中的内容在构建步骤中保持不变,并且可以在构建期间用于 docker 文件中。

    # Clone git repo.
    - name: 'gcr.io/cloud-builders/git'
      id: git-clone
      args:
      - clone
      - git@github.com:repo.git
      - workspace/repo-clone
      volumes:
      - name: 'ssh'
      path: /root/.ssh
    

    然后在 docker 文件中。

    COPY workspace/repo-name build/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多