Search code examples
node.jsdockernpmdocker-registrynpm-private-modules

docker build + private NPM (+ private docker hub)


I have an application which runs in a Docker container. It requires some private modules from the company's private NPM registry (Sinopia), and accessing these requires user authentication. The Dockerfile is FROM iojs:latest.

I have tried:

1) creating an .npmrc file in the project root, this actually makes no difference and npm seems to ignore it 2) using env variables for NPM_CONFIG_REGISTRY, NPM_CONFIG_USER etc., but the user doesn't log in.

Essentially, I seem to have no way of authenticating the user within the docker build process. I was hoping that someone might have run into this problem already (seems like an obvious enough issue) and would have a good way of solving it.

(To top it off, I'm using Automated Builds on Docker Hub (triggered on push) so that our servers can access a private Docker registry with the prebuilt images.)

Are there good ways of either: 1) injecting credentials for NPM at build time (so I don't have to commit credentials to my Dockerfile) OR 2) doing this another way that I haven't thought of ?


Solution

  • The buildkit answer is correct, except it runs everything as root which is considered a bad security practice.

    Here's a Dockerfile that works and uses the correct user node as the node Dockerfile sets up. Note the secret mount has the uid parameter set, otherwise it mounts as root which user node can't read. Note also the correct COPY commands that chown to user:group of node:node

    FROM node:12-alpine
    
    USER node
    
    WORKDIR /home/node/app
    
    COPY --chown=node:node package*.json ./
    
    RUN --mount=type=secret,id=npm,target=./.npmrc,uid=1000 npm ci
    
    COPY --chown=node:node index.js .
    
    COPY --chown=node:node src ./src
    
    CMD [ "node", "index.js" ]