在Docker容器中使用私钥时出现git Permission denied错误

3

我创建了一个Docker容器,并通过包含以下执行的Shell脚本来运行它:

docker run --rm -it \
        --user $(id -u):$(id -g) \
        --network host \
        -e USER=$USER \
        -e HOME=$HOME \
        -h $HOSTNAME \
        -v /tmp:/tmp \
        -v /home/$USER:/home/$USER \
        -v ~/.ssh:/home/$USER/.ssh:ro \
        -e USERNAME=$USERNAME \
        -w /home/$USER \
        $IMAGE_NAME \
        /bin/bash

因此,我认为既没有正确访问的问题,也没有关键存在的问题。但是,如果我进入之前使用ssh克隆的git工作目录,我无法进行身份验证。
git pull -v
git@mygitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

打印出 ssh -tv git@mygitlab.com 命令的输出结果后,我看到了以下不同之处: 没有 Docker 容器

...
debug1: Will attempt key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx agent
debug1: Will attempt key: gitlab ED25519 SHA256:fEqoFK agent
debug1: SSH2_MSG_EXT_INFO received
....
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx agent
debug1: Authentications that can continue: publickey
debug1: Offering public key: gitlab ED25519 SHA256:fEqoFK agent
debug1: Server accepts key: gitlab ED25519 SHA256:fEqoFK agent
debug1: Authentication succeeded (publickey).

在Docker容器中:
...
debug1: Will attempt key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx
debug1: Will attempt key: /home/xxx/.ssh/id_xmss 
debug1: SSH2_MSG_EXT_INFO received
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/xxx/.ssh/id_rsa
debug1: Offering public key: /home/xxx/.ssh/id_ed25519 ED25519 SHA256:20AHxx
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/xxx/.ssh/id_xmss
debug1: No more authentication methods to try.
git@mygitlab.com: Permission denied (publickey).

我需要与ssh-agent有关的东西吗?


1
根据日志显示,主机也不接受id_ed25519密钥,而是接受名为“gitlab”的密钥,但在容器中运行时未向服务器呈现。我猜测这是在主机上注册了ssh-agent - fredrik
2
虽然微不足道,但在未来会很有用:您需要 ssh -Tv-T 表示“不要使用 tty”;而 -t 则表示“要求使用 tty”,这是行不通的(但仍然可以获取您关心的信息,因此这就是为什么这很微不足道)。 - torek
2
@DavidMaze: 容器将根据容器设置重新路由文件系统、网络和/或其他请求。通常,Docker设置会重装文件系统,以便 /home/me/.ssh/ 文件与主机版本的 /home/me/.ssh/ 文件不同。在这种情况下,他直接传输了 /home/me,所以这不是问题...但他没有SSH代理文件。 - torek
2
给楼主:你的代理需要通过 $SSH_AUTH_SOCK 进行通信,这是一个 Unix 域套接字。我认为你需要在 Docker 实例内部启动一个中继代理,尽管由于你直接传递了很多内容,你可能只需将 $SSH_AUTH_SOCK 本身传递即可(我不确定这是否有效)。 - torek
@DavidMaze,显然这是在Docker容器中,因为我需要一个自定义的Ubuntu环境。 git 命令在我的主机shell上完美运行。@torek,你是对的!!将 -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK \ 添加到我的docker run脚本中解决了这个问题。你今天是我的英雄! - ywiyogo
显示剩余2条评论
1个回答

1
在这种特定情况下,您只需通过将SSH_AUTH_SOCK环境变量传递到您的Docker实例中来实现(正如您在评论中指出的那样)添加:
-e SSH_AUTH_SOCK=$SSH_AUTH_SOCK 

在更一般的情况下,如果Docker实例中没有相同的/tmp/home挂载,则此方法将无法奏效,因此您可能需要另一个ssh密钥,或者做一些更高级的事情来传递代理通信链接。但是在这里,您正在运行在相同的网络空间和文件系统上,因此只需使内部“虚拟机”1使用外部(真实)机器的ssh代理获取私钥即可。
一个 Docker 实例更像是 FreeBSD 的“jail”而不是真正的虚拟机。然而,两者都提供了一种“穷人版虚拟机”的解决方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接