简体   繁体   English

在 docker 中验证 npm 私有注册表

[英]Authenticate npm private registry in docker

We're publishing scoped js packages to a private registry (managed by us using Verdaccio).我们将作用域 js 包发布到私有注册表(由我们使用 Verdaccio 管理)。

It means that in our production environment, we need to authenticate to our private registry to use yarn install .这意味着在我们的生产环境中,我们需要对我们的私有注册表进行身份验证才能使用yarn install What's the easiest way to do it?最简单的方法是什么?

The prettier solution (IMO)更漂亮的解决方案(IMO)

Switch to yarn berry (yarn v2)切换到 yarn berry (yarn v2)

Migration guide迁移指南

Use environment variables in.yarnrc.yml:在.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您还可以将环境变量放在.env文件中并将其添加到.gitignore

Tests测试

  1. Add a scoped package添加范围 package

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

  2. Install安装

    docker-compose run --rm server yarn install

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:如果您不能或不想使用.env文件,但您的注册表 url 已经有.yarnrc.yml文件,如下所示:

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

then you can use yarn config set command to add npmAuthToken key with your token value, for example:然后您可以使用yarn config set命令添加带有令牌值的npmAuthToken键,例如:

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

RUN yarn config set 'npmScopes.yourScopeName.npmAuthToken' "$REGISTRY_TOKEN"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM