繁体   English   中英

Git 克隆 docker 容器内的私有仓库

[英]Git clone private repo inside a docker container

我有一个 docker 容器,在这个 docker 容器内,我有一个 shell 脚本,它执行来自 3492DC6D6E 的私有克隆的 ZBA9F11ECC3497D1999 脚本如下:

eval $(ssh-agent) > /dev/null
# add the ssh key
ssh-add /root/.ssh/id_rsa

kill $SSH_AGENT_PID

git clone ssh://git@bitbucket.org/project/repo.git 

但是当 docker 正在运行时,它会给出错误

Cloning into 'repo'...
Host key verification failed.

fatal: Could not read from remote repository.

当我在本地机器上测试时,我可以克隆 repo 而不会失败,所以我知道我的 ssh 密钥没有问题。

问题是当您在本地计算机上进行这样的设置时,您可以访问 SSH 的终端,将密钥添加到known_hosts询问

The authenticity of host 'server-name (***)' can't be established.
RSA key fingerprint is XXXXXXX.
Are you sure you want to continue connecting (yes/no)?

并且您基本上可以进行交互,然后键入yes和 ssh-agent 为您添加到known_hosts的连接。 但是,在这种情况下,事情发生在 docker 容器内,您基本上无法与此提示交互。 解决方案是在 ssh 配置中添加StrictHostKeyChecking no flag,对于git命令有几种方法,您可以在此处检查它们。

所以基本上,以下是解决这个问题的优雅方法,只需制作.ssh/config文件并添加我们想要的 ssh 选项。

eval $(ssh-agent) > /dev/null
# add the ssh key
ssh-add /root/.ssh/id_rsa

kill $SSH_AGENT_PID

echo "Host bitbucket.org" > /root/.ssh/config
echo "User git" >> /root/.ssh/config
echo "IdentityFile /root/.ssh/id_rsa" >> /root/.ssh/config
echo "StrictHostKeyChecking no" >> /root/.ssh/config

git clone ssh://git@bitbucket.org/project/repo.git 

StrictHostKeyChecking no option 只是丢弃提示,直接添加到known_hosts的连接,之后基本可以git clone

暂无
暂无

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

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