简体   繁体   English

使用 SSH 密钥从 docker 内部的私有存储库安装 npm 模块

[英]Using SSH key for install npm module from private repository inside docker

I make container for nodejs project.我为 nodejs 项目制作容器。 Inside the project I am using private repository.在项目内部,我使用的是私有存储库。 I need an access to it.我需要访问它。 For that I am using next Dockerfile为此,我正在使用下一个 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 file is ssh_config 文件是

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

in my package.json I added next line在我的 package.json 我添加了下一行

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

After building container using docker-compose, I login inside container and run npm i but in the end I see next error使用 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

I tried another way, I generate SSH key inside container and added public key to bitbucket, but I see the same error message.我尝试了另一种方法,我在容器内生成 SSH 密钥并将公钥添加到 bitbucket,但我看到了相同的错误消息。

You can use the host system's ssh access.您可以使用主机系统的 ssh 访问。

FROM node:15

RUN apt install openssh-client git

RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts

RUN --mount=type=ssh npm install -g typescript ts-node
WORKDIR /var/www
EXPOSE 3000

The image must be built as follows:图像必须按如下方式构建:

DOCKER_BUILDKIT=1 docker build --ssh default .

https://docs.docker.com/develop/develop-images/build_enhancements/#to-enable-buildkit-builds https://docs.docker.com/develop/develop-images/build_enhancements/#to-enable-buildkit-builds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM