dockerdockerfile

Install private package in Docker with authentication


I have an angular package in a private npm in azure and I need to install it in a docker. I don't know how to get an authentication token to connect to the private npm, I have .npmrc file.

My docker

FROM node:latest AS build
 
RUN mkdir -p /app

WORKDIR /app

COPY package.json /app

COPY .npmrc .npmrc
   
RUN npm install https://myprivate-npm/npm/registry/:_authToken=${NPM_TOKEN}

RUN npm install

When I installed it locally, I ran these two commands from VS Code and they did the job

npm install -g vsts-npm-auth --registry https://registry.npmjs.com --always-auth false    
vsts-npm-auth -config .npmrc

Solution

  • To get an access token in docker:

    Amend your .npmrc file with the following contents:

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

    Amend your dockerfile:

    FROM node:latest AS build
    ARG NPM_TOKEN
    RUN mkdir -p /app
    WORKDIR /app
    COPY package.json /app
    COPY .npmrc .npmrc
    RUN npm install
    RUN rm -f .npmrc
    

    Build your image replacing ${NPM_TOKEN} with your npm token docker build --build-arg NPM_TOKEN=${NPM_TOKEN} .

    Everything here can be found in the NPM documentation