Search code examples
dockernpmdockerfile

How to setup private npm registry authentication in dockerfile


Currently use my own private npm.

In my local, i always need to login to my own private npm first and install all the packages to my project

npm login --registry https://nodenpm.XXXX.co  # to login private npm registry
 > Enter username
 > Enter password
npm install --registry https://nodenpm.XXXX.co  # to install the deps from private registry

In the dockerfile, how do I log in to my own private npm and install?

This is my docker file

FROM ubuntu:22.04

WORKDIR /app

RUN npm login --registry=https://nodenpm.XXXX.co -u <username> -p <password>

RUN npm install --registry https://nodenpm.XXXX.co --save

RUN npm run build

EXPOSE 80
CMD [ "node", "/app/dist/main" ]

But it has fail to build in docker


Solution

  • Thank you @Branyac and based on his answer.

    Make sure you have .npmrc file

    In my docker file, I removed all the login info and added

    RUN --mount=type=secret,id=npm,target=/root/.npmrc npm install --registry https://nodenpm.XXXX.co
    

    Then in the CMD run

    docker build . -t <image name> --secret id=npm,src=$HOME/.npmrc