Docker:无法在 运行 时间在内部克隆 github 私有 rebo

Docker: Unable to clone a github private rebo inside at run time

我创建了一个容器,其中包含一个 .sh 脚本作为入口文件。此外,dockerfile 创建一个新用户,并将其主页作为工作目录。 .sh 脚本本身在新用户的工作目录中。

在运行时(docker run)可以看到容器执行了.sh,所以构建成功。

我的问题是这个容器需要克隆一个私有 github 仓库。

Before you close/vote for close /mark as duplicated this question, let me ask your help because I've googled and read over 50 different SO questions about this problem but I've not found a working example. My question is both about approach to the problem and how to implement it

我的问题是 git clone 命令告诉我:

Cloning into 'tools'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我认为我应该创建一个私钥并将其添加到我的密钥到我的 Github 配置文件中,但我无法在每个 运行 处手动添加一个新的 ssh 密钥。对吧?

可能,我应该在 构建时间 创建一个新密钥并将其添加到我的 github 存储库中。该图像将始终是私有的,因此这方面没有安全问题。但是如何做到这一点?

还有其他方法可以完成这个任务吗?

例如,我试图在 运行 时间复制我的工作私有 rsa 密钥:

docker run -it --rm my_image:git_cloning -v ~/.ssh/id_rsa:/realtebo/.ssh/id_rsa:ro

无论如何我得到了这个:

Cloning into 'tools'...
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

在构建时,我避免了 "add key problem" 执行 github keyscan

RUN mkdir ~/.ssh \
    && echo >>  ~/.ssh/known_hosts \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts 

但无论如何我在 运行 时间得到了这个:

Cloning into 'tools'...
Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

终于解决了

我将包含我的最终 Dockerfile 和 step-by-step 解释

FROM <base_image>

在这种情况下,我是从客户图片开始的,但实际上它是基于官方 ubuntu:16_04

RUN useradd -ms /bin/bash realtebo

作为第一步,我创建了新的 non-root 用户; reader 应该记住,如果不对基础 OS 图像进行任何修改,容器将只有 root 用户。

COPY id_rsa /home/realtebo/.ssh/
COPY id_rsa.pub /home/realtebo/.ssh/

我将 public 和私钥复制到 Dockerfile 文件夹中。通过这些命令,我​​有效地为新用户安装了它们

Remember: COPY works only with files/dirs in the same dir (context) of the Dockerfile!

Also: The public key must be added to your Github SSH Keys wallet

RUN chown realtebo:realtebo /home/realtebo \
    && chown realtebo:realtebo /home/realtebo/.ssh \
    && chown realtebo:realtebo /home/realtebo/.ssh/*

系统需要以新用户身份访问这些密钥,因此我需要这些 chown 命令(仅使用 1 层)。

USER realtebo

从这一点开始,我继续使用新用户进行操作

RUN echo >>  ~/.ssh/known_hosts \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts \
    && cd /home/realtebo \
    && git clone git@github.com:realtebo/my-private-tool.git tools 

第一行创建一个空的 known_hosts 文件(如果不存在),或者如果它已经存在则不附加任何内容。在 clone 时刻从 github.com 检索主机 ip 时,将从 git 使用此文件。这个主机名实际上是从他们的 DNS 系统绑定到多个 IP。

第二行将所有已知主机 public 密钥导入我们的 known_hosts 文件。

第 3 行将工作目录更改为用户的家。

第 4 行最终将我的工具真正克隆到 tools 子目录

我建议使用部署密钥而不是用户密钥,这样您就可以控制容器对存储库的访问,而无需公开您自己的密钥(如果可能)。

我也像这样共享 ssh-agent 套接字:

docker run \
--volume $SSH_AUTH_SOCK:/ssh-agent \
--env SSH_AUTH_SOCK=/ssh-agent