简体   繁体   English

如何将本地机器的 SSH 密钥传递给 docker 容器?

[英]How to pass local machine's SSH key to docker container?

I'm trying to build a docker image from Dockerfile and one of the steps that need to be taken is installing a dependency that is only available via private Gitlab repository.我正在尝试从 Dockerfile 构建 docker 映像,并且需要采取的步骤之一是安装只能通过私有 Gitlab 存储库获得的依赖项。 This means the container will need to have access to SSH keys to do the clone.这意味着容器将需要访问 SSH 密钥才能进行克隆。 I know this isn't the most secure approach, however this is only going to be an intermediate container that is going to be removed once all of the components necessary to run the app are in place.我知道这不是最安全的方法,但是这只是一个中间容器,一旦运行应用程序所需的所有组件都到位,它将被删除。

The problem is, that I cannot, whatever I try, get ssh agent inside docker to establish the connection.问题是,无论我尝试什么,我都无法在 docker 中获取 ssh 代理来建立连接。 I get:我得到:

npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

The same thing happens if I try to simply clone the repository without running npm install .如果我尝试简单地克隆存储库而不运行npm install ,也会发生同样的事情。 Here is the Dockerfile I use:这是我使用的 Dockerfile:

FROM risingstack/alpine:3.4-v6.9.4-4.2.0


RUN apk update

RUN apk add openssh

ARG SSH_KEY

# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
    chmod 700 /root/.ssh && \
    ssh-keyscan github.com > /root/.ssh/known_hosts

# Add the keys and set permissions
RUN echo "$SSH_KEY" > /root/.ssh/id_rsa && \
    chmod 700 /root/.ssh/id_rsa && \


RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa && ssh -o StrictHostKeyChecking=no git@github.com || true && npm install

and the command (I pass the private key as build argument):和命令(我将私钥作为构建参数传递):

docker build -t test  --build-arg SSH_KEY="$(cat ~/.ssh/id_rsa)" .

This works for me :这对我有用:

Using this workaround : https://stackoverflow.com/a/47544999/3957754 to pass files as build args使用此解决方法: https : //stackoverflow.com/a/47544999/3957754将文件作为构建参数传递

Dockerfile文件

ARG SSH_KEY
ENV SSH_KEY=$SSH_KEY

# Make ssh dir
RUN mkdir /root/.ssh/
 
# Create id_rsa from string arg, and set permissions

RUN echo "$SSH_KEY" > /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa
 
# Create known_hosts
RUN touch /root/.ssh/known_hosts

# Add git providers to known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
RUN ssh-keyscan gitlab.com >> /root/.ssh/known_hosts

Build建造

docker build -t some-app --build-arg SSH_KEY="$(cat ~/file/outside/build/context/id_rsa)" .

With this, you can perform git clone git@github.com... (gitlab, or bitbucket) at build stage or at run stage using ENTRYPOINT ["docker-entrypoint.sh"] .有了这个,您可以在构建阶段或运行阶段使用ENTRYPOINT ["docker-entrypoint.sh"]执行git clone git@github.com... (gitlab 或 bitbucket)。

Update更新

This could works if you need to pass any file as parameter to your container如果您需要将任何文件作为参数传递给容器,这可能会起作用

I'd clone it on the host, using the ssh-agent you already have running, before you run docker build .在运行docker build之前,我会使用您已经运行的 ssh-agent 在主机上克隆它。

If you really have to have the private key in the image (which you've acknowledged is dangerous) then you should be able to have it at its default location $HOME/.ssh/id_rsa where you have it in your code;如果您确实必须在映像中拥有私钥(您已经承认这是危险的),那么您应该能够将其放在代码中的默认位置$HOME/.ssh/id_rsa don't try to launch an ssh-agent.不要尝试启动 ssh-agent。 You could also inject a $HOME/.ssh/config file if your problem is aggressive host key checking, or a $HOME/.ssh/known_hosts file that has the host key already.如果您的问题是积极的主机密钥检查,您也可以注入一个$HOME/.ssh/config文件,或者已经有主机密钥的$HOME/.ssh/known_hosts文件。 Since all of these are files you might find it easier to have them in the Docker build tree and COPY them into $HOME/.ssh .由于所有这些都是文件,您可能会发现将它们放在 Docker 构建树中并将它们COPY$HOME/.ssh会更容易。

build a docker image from Dockerfile and one of the steps that need to be taken is installing a dependency that is only available via private Gitlab repository从 Dockerfile 构建 docker 映像,需要采取的步骤之一是安装仅可通过私有 Gitlab 存储库获得的依赖项

Like described in much more detail in my answer to How to mount host volumes into docker containers in Dockerfile during build , Docker also provides SSH agent forwarding. Like described in much more detail in my answer to How to mount host volumes into docker containers in Dockerfile during build , Docker also provides SSH agent forwarding. This uses the --ssh flag in the docker build command, along with --mount=type=ssh in any Dockerfile's RUN command for which you want SSH authentication to be delegated to the agent on the host.这使用docker build命令中的--ssh标志,以及您希望将 SSH 身份验证委托给主机上的代理的任何 Dockerfile 的RUN命令中的--mount=type=ssh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM