[英]Docker: Unable to clone a github private rebo inside at run time
我创建了一个带有.sh脚本作为条目文件的容器。 此外,dockerfile创建一个新用户,其home为工作目录。 .sh脚本本身位于新用户的工作目录中。
在运行时( docker run
),我可以看到容器执行.sh,因此构建成功。
我的问题是这个容器需要克隆一个私有的github repo。
在你关闭/投票关闭/标记为重复这个问题之前,让我问你的帮助,因为我已经用Google搜索并阅读了50多个关于这个问题的SO问题,但我没有找到一个有效的例子。 我的问题是关于问题的解决方法以及如何实施
我的问题是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.
在构建时,我避免了执行github密钥扫描的“添加密钥问题”
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以及逐步说明
FROM <base_image>
在这种情况下,我从一个客户形象开始,但在起源它是基于官方的ubuntu:16_04
RUN useradd -ms /bin/bash realtebo
作为第一个动作,我创建了新的非root用户; 读者应该记住,如果没有对基本操作系统映像进行任何修改,容器将只有root
用户。
COPY id_rsa /home/realtebo/.ssh/
COPY id_rsa.pub /home/realtebo/.ssh/
我将公钥和私钥复制到Dockerfile文件夹中。 通过这些命令,我有效地为新用户安装了它们
请记住:
COPY
仅适用于Dockerfile的同一目录(上下文)中的文件/目录!另外:必须将公钥添加到您的Github SSH Keys钱包中
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。
第二行将所有已知的主机公钥导入我们的known_hosts
文件。
第3行将工作目录更改为用户的家。
第4行终于将我的工具真正克隆到tools
子目录中
我建议使用部署密钥而不是用户密钥,这样您就可以控制容器对存储库的访问,而不会暴露您自己的密钥。
我也做了类似的事情,像这样共享ssh-agent套接字:
docker run \ --volume $SSH_AUTH_SOCK:/ssh-agent \ --env SSH_AUTH_SOCK=/ssh-agent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.