【问题标题】:Using SSH key for install npm module from private repository inside docker使用 SSH 密钥从 docker 内部的私有存储库安装 npm 模块
【发布时间】:2022-03-12 07:39:28
【问题描述】:

我为 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.js docker npm ssh private-key


    【解决方案1】:

    您可以使用主机系统的 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
    

    图像必须按如下方式构建:

    DOCKER_BUILDKIT=1 docker build --ssh default .
    

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

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2020-11-20
      • 2022-07-22
      • 2021-11-04
      • 2014-11-15
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      相关资源
      最近更新 更多