When trying to
docker build \
--tag my-app:latest \
.
with the following 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"]
And the following .npmrc
:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
I receive the following error during step Step 6/14 : RUN ["npm", "install"]
of the intermediate
container:
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)
I also have the following line in my .bashrc
for my Bash shell and .zshrc
for my Z shell:
export NPM_TOKEN=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
I am adding the node_modules
to my Docker Image in this fashion in accordance with the official NPM documentation found here.
You're almost there!
According to the official NPM documentation you linked, all you're missing from your docker build
command is the --build-arg
option, mentioned here and detailed here.
Your finished command should be:
docker build \
--build-arg NPM_TOKEN=${NPM_TOKEN} \
--tag my-app:latest \
.