Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticate npm private registry in docker

We're publishing scoped js packages to a private registry (managed by us using Verdaccio).

It means that in our production environment, we need to authenticate to our private registry to use yarn install. What's the easiest way to do it ?

like image 513
mbesson Avatar asked Sep 02 '25 17:09

mbesson


2 Answers

The prettier solution (IMO)

Switch to yarn berry (yarn v2)

Migration guide

Use environment variables in .yarnrc.yml :

yarnPath: ".yarn/releases/yarn-berry.cjs"
nodeLinker: node-modules
npmScopes:
  customScope:
    npmRegistryServer: ${NPM_REGISTRY}
    npmAlwaysAuth: true
    npmAuthToken: ${NPM_TOKEN}

Set environment variable values

#docker-compose.yml
version: '3.7'
services:
  server:
    image: node:14
    environment:
      NPM_REGISTRY=https://private-registry
      NPM_TOKEN=PUT_YOUR_TOKEN_HERE
    ports:
      - "3000:3000"
    volumes:
      - .:/var/app
    command: "yarn run dev"

You can also put env vars in a .env file and add it to .gitignore

Tests

  1. Add a scoped package

    docker-compose run --rm server yarn add @customScope/test-package

  2. Install

    docker-compose run --rm server yarn install

like image 165
mbesson Avatar answered Sep 04 '25 09:09

mbesson


If you can't or don't want to use .env file, but you already have .yarnrc.yml file with your registry url like that:

npmScopes:
  yourScopeName:
    npmRegistryServer: 'https://...'

then you can use yarn config set command to add npmAuthToken key with your token value, for example:

// $REGISTRY_TOKEN is an ARG variable you pass to Docker
ARG REGISTRY_TOKEN

RUN yarn config set 'npmScopes.yourScopeName.npmAuthToken' "$REGISTRY_TOKEN"
like image 43
Danila Avatar answered Sep 04 '25 10:09

Danila