首页 > 解决方案 > 使用 SSH 密钥从 docker 内部的私有存储库安装 npm 模块

问题描述

我为 nodejs 项目制作容器。在项目内部,我使用的是私有存储库。我需要访问它。为此,我正在使用下一个 Dockerfile

FROM node:15

RUN echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
COPY keys/ssh_config /root/.ssh/config
COPY keys/bitbucket /root/.ssh/bitbucket
RUN chmod 600 /root/.ssh/bitbucket

RUN npm install -g typescript ts-node
WORKDIR /var/www
EXPOSE 3000

ssh_config 文件是

Host bitbucket.org
     IdentityFile /root/.ssh/bitbucket
     StrictHostKeyChecking no

在我的 package.json 我添加了下一行

"my-interfaces": "git+ssh://bitbucket.org:MY_USER_NAME/my-interfaces.git#master",

使用 docker-compose 构建容器后,我登录容器并运行 npm i ,但最后我看到下一个错误

npm ERR! code 128
npm ERR! command failed
npm ERR! command git ls-remote ssh://git@bitbucket.org/MY_USER_NAME/my-interfaces.git
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-04-30T08_19_38_999Z-debug.log

我尝试了另一种方法,我在容器内生成 SSH 密钥并将公钥添加到 bitbucket,但我看到了相同的错误消息。

标签: node.jsdockernpmsshprivate-key

解决方案


推荐阅读