如何快速用docker搭建npm私服

世上功夫,唯快不破!

npm包默认下载的官方服务器是 npmjs:  https://registry.npmjs.org/

由于在国外以及众所周知的原因,npm install直接下会慢到怀疑人生。。。

幸好阿里提供了国内淘宝镜像源  taobao:   https://registry.npm.taobao.org/    (马首富还是有不少贡献的)

我们只需要在本地敲命令切换下npm包源

设置npm源
npm config set registry https://registry.npm.taobao.org/

查看是否生效
npm config get registry

再用npm install速度快到感动。。。

但是,随着前端node项目越来越多,越来越复杂,且每天gitlab频繁的构建让我发现,有时候npm install也要花个10分钟,不行,邪念一起,必须搭建npm私服。。。。

然后各种搜索让我搜到npm私服神器

verdaccio

官网文档写的很清楚,还有docker镜像,简直爽到Hi, 废话不多说,上干货

第一步:拉取verdaccio的Docker镜像

docker pull verdaccio/verdaccio

第二部:运行verdaccio的Docker镜像

 

docker run --restart=always --name verdaccio -v /opt/verdaccio/conf:/verdaccio/conf -v /opt/verdaccio/storage:/verdaccio/storage -p 4873:4873 -d verdaccio/verdaccio

  挂载的目录你可以自己设定

  /opt/verdaccio/storage目录挂载到镜像的/verdaccio/storage目录, 这是用于缓存本地的npm包的目录,即从外网下载过的npm包都会存在这个目录下,这样下次再npm install时就不用访问外网了。

  注:要确保/opt/verdaccio/storage目录加了写权限,你可以输入命令 

sudo chmod -R o+w /opt/verdaccio/storage

   /opt/verdaccio/conf 目录挂载到镜像的 /verdaccio/conf目录, 这是用于放入配置文件config.yaml的目录,该文件内容如下 (我是根据默认配置文件修改,将npmjs改成了taobao)

#
# This is the config file used for the docker images.
# It allows all users to do anything, so don't use it on production systems.
#
# Do not configure host and port under `listen` in this file
# as it will be ignored when using docker.
# see https://verdaccio.org/docs/en/docker#docker-and-custom-port-configuration
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
storage: /verdaccio/storage/data
# path to a directory with plugins to include
plugins: /verdaccio/plugins

web:
  # WebUI is enabled as default, if you want disable it, just uncomment this line
  #enable: false
  title: Verdaccio
  # comment out to disable gravatar support
  # gravatar: false
  # by default packages are ordercer ascendant (asc|desc)
  # sort_packages: asc
  # darkMode: true
  # logo: http://somedomain/somelogo.png
  # favicon: http://somedomain/favicon.ico | /path/favicon.ico

# translate your registry, api i18n not available yet
# i18n:
# list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations
#   web: en-US

auth:
  htpasswd:
    file: /verdaccio/storage/htpasswd
    # Maximum amount of users allowed to register, defaults to "+infinity".
    # You can set this to -1 to disable registration.
    # max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  taobao:
    url: https://registry.npm.taobao.org/
  npmjs:
    url: https://registry.npmjs.org/

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: taobao 

  '**':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish/publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated
    unpublish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: taobao

# You can specify HTTP/1.1 server keep alive timeout in seconds for incoming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enough.
server:
  keepAliveTimeout: 60

middlewares:
  audit:
    enabled: true

# log settings
logs: { type: stdout, format: pretty, level: http }

#experiments:
#  # support for npm token command
#  token: false
#  # support for the new v1 search endpoint, functional by incomplete read more on ticket 1732
#  search: false

# This affect the web and api (not developed yet)
#i18n:
#web: en-US

 

第二部:验证verdaccio

 假设私服地址 192.168.1.8

  浏览器输入 http://192.168.1.8:4873 , 可以看到如下界面

在本地电脑找个前端项目,删掉node_modules, 并输入以下命令清除本地缓存

npm cache clear --force

然后修改本地的npm源

npm config set registry http://192.168.1.8:4873

然后 npm install 

再登录到私服服务器,看看/opt/verdaccio/storage目录下是否出现data目录,进入data目录就可以看到逐步有一些npm包出现了,说明配置成功

第一次使用私服估计慢,后面应该下过的包,再下就应该飞快了。。。。。

https://verdaccio.org/docs/en/docker

https://www.codenong.com/j5dfa13f16fb9a016077/

https://www.cnblogs.com/dearxinli/p/11170359.html

posted on 2021-05-26 14:27  omage  阅读(966)  评论(0)    收藏  举报