skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. It’s not recommended to include user and password credentials directly inside a Dockerfile due to security risks. Instead, you should store npm registry credentials in the .npmrc1 file.

    You should remove npm login from your dockerfile and mount the .npmrc file as a Docker secret in the correct path (Typically, the npmrc file is located at $HOME/.npmrc).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search