将文件从Google Cloud Container Builder传递到Docker构建任务

Ale*_*ann 5 docker google-container-builder

语境

使用Container Builder构建并用于App Engine的Ruby on Rails应用程序。我们要求捆绑器能够使用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]']
Run Code Online (Sandbox Code Playgroud)

一些标识符已被省略

deploy / 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
Run Code Online (Sandbox Code Playgroud)

Docker文件

# .. 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
Run Code Online (Sandbox Code Playgroud)

问题:

构建任务(使用构建触发器运行)失败,并显示以下信息:

...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
Run Code Online (Sandbox Code Playgroud)

我感觉我不了解Container Builder和Docker共享卷和数据的方式。

Von*_*onC 5

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

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

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

但是,您无法一次装入卷docker build(请参见moby问题14080),因此请检查此解决方案:多阶段构建可以提供帮助