首页 > 解决方案 > Docker npm install 因本地依赖项而失败,但在命令行中工作

问题描述

我正在尝试对接一个项目,该项目使用我们 GitLab 服务器上的一些私有 npm 依赖项(url 类似于 10.1.1.150)。当我在 CMD 中运行 npm install 时,它运行良好,但在 Docker 中我收到以下错误:

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t https://10.1.1.150/WebDev/firstdependency.git
npm ERR!
npm ERR! remote: HTTP Basic: Access denied
npm ERR! fatal: Authentication failed for 'https://10.1.1.150/WebDev/firstdependency.git/'
npm ERR!
npm ERR! exited with error code: 128

我的 Dockerfile:

FROM node
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm set strict-ssl false --global
RUN git init
RUN git config --global http.sslVerify false
RUN git config http.emptyAuth true
RUN npm install --production
RUN mv node_modules ../
COPY . .
CMD npm start

包含失败部分的 package.json 文件的一部分:

"repository": {
    "type": "git",
    "url": "https://10.1.1.150/WebDev/thisproject.git"
  },
  "dependencies": {
    "@mycompany/firstdependency": "git+https://10.1.1.150/WebDevelopment/firstdependency.git",
    "@mycompany/seconddependency": "git+https://10.1.1.150/WebDevelopment/seconddependency.git",
    "@mycompany/thirddependency": "git+https://10.1.1.150/WebDevelopment/thirddependency.git"

我想问题的核心是我无法在 Dockerfile 中传递我的凭据,因此它无法登录到我们的本地仓库。操作系统:WIN10 npm:6.13.4 Docker:19.03.2 如何解决此身份验证错误?

标签: gitdockerauthenticationnpmgitlab

解决方案


我假设您正在通过 ssh 私有认证对您的私有 npm 注册表进行身份验证。在这种情况下,您将需要使用Docker Build Enhancements

以下是您需要更新的一些内容:

  • 在您的Dockerfile中,将此行添加为第一行:# syntax=docker/dockerfile:experimental
  • 更改RUN npm install --productionRUN --mount=type=ssh npm install --production
  • 之后,您可以像docker build --ssh <image_name> .

新的Dockerfile

# syntax=docker/dockerfile:experimental

FROM node
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm set strict-ssl false --global
RUN git init
RUN git config --global http.sslVerify false
RUN git config http.emptyAuth true

RUN --mount=type=ssh npm install --production

RUN mv node_modules ../
COPY . .
CMD npm start

编辑 1

由于您将凭据设置为环境变量(您应该考虑将其更改为使用 ssh 证书),这是我建议的解决方案。

  1. 准备一个包含用于登录 git 的环境名称和值的文件。假设文件位于 /gitsecret.txt ,内容如下
GIT_USERNAME=<value>
GIT_PASSWORD=<value>
  1. 更新你Dockerfile
# syntax=docker/dockerfile:experimental

# Builder layer
FROM node as builder

ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm set strict-ssl false --global
RUN git init
RUN git config --global http.sslVerify false
RUN git config http.emptyAuth true
RUN --mount=type=secret,id=gitsecret \
    set -a; \
    source /run/secrets/gitsecret; \
    set +a; \
    npm install --production

# Main layer
FROM node

WORKDIR /usr/src/app

RUN mv node_modules ../

COPY --from=builder /usr/src/app/node_modules /usr/src/node_modules
COPY . .

CMD npm start
  1. 现在您可以运行 docker build 命令,例如
docker build --secret id=gitsecret,src=/gitsecret.txt <image_name> .

这种方式是为了确保:

  • 该密钥只能在构建阶段使用。构建层将从最终映像中删除,因此最终映像中不会保留任何秘密。
  • 在构建过程中不会打印出秘密内容。

注意我没有您的完整源代码,因此不能保证仅通过复制和粘贴代码就可以适用于您的情况。但是,这个想法就在那里,所以请相应地更新它!


推荐阅读