【问题标题】:Git clone private repo inside a docker containerGit 克隆 docker 容器中的私有仓库
【发布时间】:2021-02-05 21:34:52
【问题描述】:

我有一个 docker 容器,在这个 docker 容器内我有一个 shell 脚本,它从一个私有仓库执行 git 克隆。脚本如下:

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 密钥没有问题。

【问题讨论】:

    标签: linux git docker ssh rsa


    【解决方案1】:

    问题是当您在本地计算机上进行这样的设置时,您可以访问终端进行 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 标志,git 命令有几种方法,您可以检查它们here

    所以基本上,以下是解决这个问题的优雅方法,只需制作 .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选项只是丢弃提示,直接将连接添加到known_hosts,之后基本可以git clone

    【讨论】:

    • 关闭主机检查会使您容易受到中间人攻击。
    • 好吧,我很想听听更好的选择。实际上这是我的工作,但我不确定这是否是最佳实践,尽管对我来说这个容器在 AWS ECR 中,它的权限策略很严格,所以应该没问题,但是当我在本地尝试时,它可能会出现漏洞。
    • @PresidentJamesK.Polk 有什么建议吗?
    • 不,除了提前验证密钥并将它们放在 known_hosts 中。主机密钥应该很少更改。许多用户只是在提示符处单击“是”而不检查,因此将 StrictHostKeyChecking 设置为 no 不会让事情变得更糟。我的评论只是为了记录一个安全事实。
    猜你喜欢
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2015-06-12
    • 2015-08-11
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多