docker build 期间的 SSH 代理转发 [英] SSH agent forwarding during docker build
问题描述
在通过 dockerfile
构建 docker 映像时,我必须克隆一个 github 存储库.我将我的公共 ssh 密钥添加到了我的 git hub 帐户中,并且我能够从我的 docker 主机克隆 repo.虽然我看到我可以通过在 docker run like 时映射 $SSH_AUTH_SOCK
env 变量来使用 docker 主机的 ssh 密钥
While building up a docker image through a dockerfile
, I have to clone a github repo. I added my public ssh keys to my git hub account and I am able to clone the repo from my docker host. While I see that I can use docker host's ssh key by mapping $SSH_AUTH_SOCK
env variable at the time of docker run like
docker run --rm -it --name container_name
-v $(dirname $SSH_AUTH_SOCK):$(dirname $SSH_AUTH_SOCK)
-e SSH_AUTH_SOCK=$SSH_AUTH_SOCK my_image
如何在 docker build 期间做同样的事情?
How can I do the same during a docker build?
推荐答案
适用于 Docker 18.09 及更新版本
您可以使用 Docker 的新功能将现有的 SSH 代理连接或密钥转发给构建器.例如,这可以在构建期间克隆您的私有存储库.
For Docker 18.09 and newer
You can use new features of Docker to forward your existing SSH agent connection or a key to the builder. This enables for example to clone your private repositories during build.
步骤:
首先设置环境变量以使用新的 BuildKit
First set environment variable to use new BuildKit
export DOCKER_BUILDKIT=1
然后使用新的(实验性)语法创建 Dockerfile:
Then create Dockerfile with new (experimental) syntax:
# syntax=docker/dockerfile:experimental
FROM alpine
# install ssh client and git
RUN apk add --no-cache openssh-client git
# download public key for github.com
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
# clone our private repository
RUN --mount=type=ssh git clone git@github.com:myorg/myproject.git myproject
并使用构建图像
docker build --ssh default .
在此处了解更多信息:https://medium.com/@tonistiigi/build-secrets-and-ssh-forwarding-in-docker-18-09-ae8161d066
这篇关于docker build 期间的 SSH 代理转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!