【问题标题】:Docker npm install fails with local dependencies, but works in command lineDocker npm install 因本地依赖项而失败,但在命令行中工作
【发布时间】:2020-02-14 10:58:21
【问题描述】:

我正在尝试 dockerize 一个项目,该项目使用我们 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 中传递我的凭据,因此它无法登录到我们的本地 repo。 操作系统:WIN10 npm:6.13.4 码头工人:19.03.2 如何解决此身份验证错误?

【问题讨论】:

    标签: git docker authentication npm gitlab


    【解决方案1】:

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

    您需要更新以下几点:

    • 在您的Dockerfile 中,将此行添加为第一行:# syntax=docker/dockerfile:experimental
    • RUN npm install --production 更改为RUN --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> .
    

    这种方式是为了确保:

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

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

    【讨论】:

    • 感谢您的快速回复!实际上我们是通过 https 连接到它的,并且没有设置 ssh。
    • 我明白了,你能把你用来连接注册表的命令发给我吗?
    • 我们为此使用了环境变量,例如,如果我执行git clone https://10.1.1.150/WebDev/whatever.git,它会克隆它而无需输入我的用户名或密码。
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2022-12-04
    • 2016-02-22
    • 2019-02-23
    • 2018-10-12
    相关资源
    最近更新 更多