【发布时间】:2016-03-25 08:18:05
【问题描述】:
我正在尝试从 Docker 容器中的 npm 克隆私有 Git 存储库,但收到错误 Permission denied (publickey)。
我在主机上使用此命令生成了一个 SSH 密钥:
ssh-keygen -t rsa -b 4096 -C "myname@mydomain.com"
然后我将它添加到 Bitbucket,ssh-add-ed 它,并用 git clone 对其进行了测试,一切正常(在主机上)。我还在我的项目文件夹中尝试了npm install,所有 git 克隆都很好。
我将id_rsa 和id_rsa.pub 文件复制到另一个文件夹,并在执行docker run 时将此文件夹挂载到/root/.ssh。现在的问题是npm install 在尝试从该私有 Git 存储库克隆时给出了权限被拒绝错误。
我用 bash 进入容器并手动尝试了npm install,但得到了同样的错误。我通过运行eval "$(ssh-agent -s)" 确保ssh-agent 已启动,然后通过运行ssh-add ~/.ssh/id_rsa 确保添加了密钥。然后我又试了一次,还是同样的问题。
但问题是,如果在同一个容器中,我只尝试一个普通的git clone ...,那么它克隆得很好!没有问题或任何东西。我知道它正在使用 SSH 密钥,因为如果我从 Bitbucket 中删除它,我会收到权限被拒绝错误。然后,如果我将它重新添加到 Bitbucket,它会很好地克隆。
如何让 npm 正确克隆?我使用的工作(在容器内)git clone 命令是:
git clone git@bitbucket.org:org/repo.git
并且 npm repo 依赖行是:
"repo": "git+ssh://bitbucket.org/org/repo.git"
完整的 npm 错误是:
npm ERR! git clone ssh://bitbucket.org/org/repo.git Cloning into bare repository '/root/.npm/_git-remotes/ssh-bitbucket-org-org-repo-git-3f459951'...
npm ERR! git clone ssh://bitbucket.org/org/repo.git Permission denied (publickey).
npm ERR! git clone ssh://bitbucket.org/org/repo.git fatal: Could not read from remote repository.
npm ERR! git clone ssh://bitbucket.org/org/repo.git
npm ERR! git clone ssh://bitbucket.org/org/repo.git Please make sure you have the correct access rights
npm ERR! git clone ssh://bitbucket.org/org/repo.git and the repository exists.
【问题讨论】: