0

My JavaScript project is successfully harnessing a multi stage build process within a Dockerfile. During the "build" stage a .npmrc file is being built on the fly using a secret supplied as a Docker build arg, as illustrated below:

FROM node:12.16-alpine AS build
ARG ACCESS_TOKEN

WORKDIR /app
COPY package*.json ./

RUN echo "//npm.pkg.github.com/:_authToken=$ACCESS_TOKEN" > .npmrc && \
    echo "@my_org:registry=https://npm.pkg.github.com" >> .npmrc && \
    npm ci --production && \
    rm -f .npmrc

# ...remainder of file omitted

I know that I can supply the token at the command line as follows: NPM_TOKEN=$ACCESS_TOKEN npm ci --production. However, this fails as follows:

... earlier docker build output omitted
 ---> 68c5ba096f60
Step 6/16 : RUN NPM_TOKEN=$ACCESS_TOKEN npm ci --production --registry https://npm.pkg.github.com/
 ---> Running in 418c51a85c15
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-09-09T11_07_17_789Z-debug.log
failed to build: couldn't build "my_org/my-project": unable to stream build output: The command '/bin/sh -c NPM_TOKEN=$ACCESS_TOKEN npm ci --production --registry https://npm.pkg.github.com/' returned a non-zero code: 1

Is what I am attempting possible or must there always be a .npmrc file somewhere that is used for authentication?

Mark
  • 1,884
  • 2
  • 25
  • 36

1 Answers1

-1

Did you try by setting as ENV before the the npm cmd?

ENV NPM_TOKEN=$ACCESS_TOKEN

And in your command line you are passing NPM_TOKEN=$ACCESS_TOKEN, I think it should be ACCESS_TOKEN=${NPM_TOKEN} as the ARG variable in dockerfile is ACCESS_TOKEN

https://docs.npmjs.com/docker-and-private-modules#update-the-dockerfile

Bimal
  • 1,175
  • 8
  • 16
  • `ENV` variables are for runtime only; I need this to happen during `docker build ...`. See [npm documentation](https://docs.npmjs.com/docker-and-private-modules#background-runtime-variables) for more info. – Mark Sep 10 '20 at 06:59