上下文
使用 Container Builder 构建的用于 App Engine 的 Ruby on Rails 应用程序。我们要求 bundler 能够使用 SSH key 从私有(private) git 存储库安装依赖项。
SSH key 来自安全存储桶,并通过 KMS 进行解密。这些步骤没问题。但是,使用 Docker 构建容器的最后一步没有失败,无法访问 SSH key 。
我之前没有任何使用 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]']
省略了一些标识符
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
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 共享卷和数据的方式。
最佳答案
Dockerfile COPY
可以使用多个<src>
资源,但文件和目录的路径将被解释为相对于构建上下文的源。
这是您执行 docker build .
的当前路径命令。
在您的情况下,如果在 Dockerfile 执行其步骤时安装了/root/.ssh,则简单的 RUN cp /root/.ssh/... /destination/path
就足够了。
但是,您无法在 docker build
挂载卷时间(参见 moby issue 14080 ),因此检查 this solution: a multi-stage build can help .
关于docker - 将文件从 Google Cloud Container Builder 传递到 Docker 构建任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48274288/