如何在中间容器中使用NPM配置文件?

How Do I Use an NPM Configuration File In an Intermediate Container?

尝试时

docker build \
--tag my-app:latest \
.

与以下 Dockerfile:

FROM node:9-stretch AS intermediate

ARG NPM_TOKEN

COPY [".npmrc", "./"]

RUN ["chmod", "0600", ".npmrc"]

COPY ["package.json", "./"]

RUN ["npm", "install"]

FROM node:9-stretch

WORKDIR /usr/src/app/

COPY --from=intermediate ["node_modules/.", "./node_modules/."]

COPY ["package.json", "index.js", "./"]

CMD ["node", "index.js"]

以及以下.npmrc

//registry.npmjs.org/:_authToken=${NPM_TOKEN}

我在 intermediate 容器的步骤 Step 6/14 : RUN ["npm", "install"] 中收到以下错误:

Error: Failed to replace env in config: ${NPM_TOKEN}
    at /usr/local/lib/node_modules/npm/lib/config/core.js:417:13
    at String.replace (<anonymous>)
    at envReplace (/usr/local/lib/node_modules/npm/lib/config/core.js:413:12)
    at parseField (/usr/local/lib/node_modules/npm/lib/config/core.js:391:7)
    at /usr/local/lib/node_modules/npm/lib/config/core.js:334:17
    at Array.forEach (<anonymous>)
    at Conf.add (/usr/local/lib/node_modules/npm/lib/config/core.js:333:23)
    at ConfigChain.addString (/usr/local/lib/node_modules/npm/node_modules/config-chain/index.js:244:8)
    at Conf.<anonymous> (/usr/local/lib/node_modules/npm/lib/config/core.js:321:10)
    at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:525:3)
/usr/local/lib/node_modules/npm/lib/npm.js:61
      throw new Error('npm.load() required')
      ^

Error: npm.load() required
    at Object.get (/usr/local/lib/node_modules/npm/lib/npm.js:61:13)
    at process.errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:205:18)
    at process.emit (events.js:180:13)
    at process._fatalException (internal/bootstrap/node.js:391:27)

我的 Bash shell 的 .bashrc 和 Z shell 的 .zshrc 中也有以下行:

export NPM_TOKEN=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

我正在按照找到的 here.

官方 NPM 文档以这种方式将 node_modules 添加到我的 Docker 图像中

你就快完成了!

根据您链接的官方 NPM 文档,您的 docker build 命令只缺少 --build-arg 选项,提到了 here and detailed here

您完成的命令应该是:

docker build \
--build-arg NPM_TOKEN=${NPM_TOKEN} \
--tag my-app:latest \
.