git - 从 Dockerfile 克隆私有(private) git 存储库

标签 git docker ssh ssh-keys

我的目标是从 Dockerfile 克隆私有(private)存储库。我复制我的私有(private) SSH key ,然后将 bitbucket.org 域添加到 known_hosts 文件中,但是当我尝试克隆存储库时,由于某种原因,我收到“权限被拒绝”错误。不过,我可以从我的主机克隆这个存储库。我错过了什么?

Dockerfile:

FROM ubuntu

RUN apt-get update
RUN apt-get install -y git ssh

ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa

RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts

RUN git clone <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f08061b2f0d061b0d1a0c040a1b41001d08" rel="noreferrer noopener nofollow">[email protected]</a>:foo/bar.git

命令:

docker build --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" .

输出:

Warning: Permanently added the RSA host key for IP address '18.205.93.2' to the list of known hosts.
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bece2ffcbe9e2ffe9fee8e0eeffa5e4f9ec" rel="noreferrer noopener nofollow">[email protected]</a>: Permission denied (publickey).
fatal: Could not read from remote repository.

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

运行 ssh -v [email protected]输出:

OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
Pseudo-terminal will not be allocated because stdin is not a terminal.
debug1: Connecting to bitbucket.org [18.205.93.0] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0, remote software version conker_b9a79bcd5e-dirty conker-3002
debug1: no match: conker_b9a79bdd5e-dirty conker-3002
debug1: Authenticating to bitbucket.org:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6cb958e87d4d3d0a68a8f8495958ec8899481" rel="noreferrer noopener nofollow">[email protected]</a>
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8eede6efede6efbcbea3fee1e2f7bfbdbebbcee1feebe0fdfde6a0ede1e3" rel="noreferrer noopener nofollow">[email protected]</a> MAC: <implicit> compression: none
debug1: kex: client->server cipher: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284b40494b40491a180558474451191b181d6847584d465b5b40064b4745" rel="noreferrer noopener nofollow">[email protected]</a> MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:zzXQOXSRBEiUtuEwbHaxvSc0ojez6sdf6s9YXaGp1A
debug1: Host 'bitbucket.org' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
Warning: Permanently added the RSA host key for IP address '18.205.93.0' to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4c425f6b49425f495e48404e5f0544594c" rel="noreferrer noopener nofollow">[email protected]</a>: Permission denied (publickey).

最佳答案

您需要确保 id_rsa 填写正确:

docker build --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" .

编辑 这仅适用于没有密码的 key ,否则事情会变得复杂。

关于git - 从 Dockerfile 克隆私有(private) git 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64459987/

相关文章:

docker - Dockerfile 中的条件 ENV

端口更改后 SSH 连接到 EC2 实例失败

linux - SCP 错误 : Bad configuration option: PermitLocalCommand

git - gpg : skipped "N": secret key not available

Git 如何从克隆副本重建丢失的中央存储库

git - 为什么 "git pull"有时会要求我提供 merge 请求注释?

linux - 通过代理ssh错误使用Docker

python - 使用 Selenium 和 Python 在 Chrome 中同时登录多个帐户

windows - svn+ssh结账慢

Git diff 工具重置为 git default