记录docker verdaccio搭建私有npm库

1,458 阅读3分钟

1.安装docker

1.安装docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

docker -v 检查docker是否安装成功

2.安装docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.6/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

docker-compose -v 检查docker-compose是否安装成功

如出现 -bash: /usr/local/bin/docker-compose: Permission denied 权限不足

chmod +x /usr/local/bin/docker-compose

3.编写docker-compose.yml

/verdaccio/docker-compose.yml

version: '3.1'
services:
  verdaccio:
    image: verdaccio/verdaccio
    container_name: "verdaccio"
    networks:
      - node-network
    environment:
      - VERDACCIO_PORT=4873
    ports:
      - "4873:4873"
    volumes:
      - "./storage:/verdaccio/storage"
      - "./config:/verdaccio/conf"
      - "./plugins:/verdaccio/plugins"  
networks:
  node-network:
    driver: bridge

4. 编写 ./config/config.yaml

#
# 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
# 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:
  npmjs:
    url: https://registry.npmjs.org/
packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs
  '**':
    # 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: npmjs
middlewares:
  audit:
    enabled: true
# log settings
logs:
  - { type: stdout, format: pretty, level: http }
  #- {type: file, path: verdaccio.log, level: info}
#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

5.执行 docker-compose up -d 持续运行

docker-compose up -d 打开浏览器 ip:4873 正常看到页面

6.添加用户

npm adduser --registry your.npm-server.com

根据提示输入用户名和密码,最后却提示 500 服务器错误。 我们在服务器上执行以下命令查看容器日志:

docker logs --tail 20 verdaccio 发现: EACCES: permission denied, open '/verdaccio/storage/htpasswd' 原来是没有权限。查了一番资料得知,用户在新增 npm 用户的时候会写入 htpasswd 文件,由于该文件是在宿主机中,默认是 root 用户建立的,而 verdaccio 容器中拥有自己的用户名,名字就叫 verdaccio,所以无法写入 root 用户拥有的文件。 那么是不是还要在宿主机上新建 verdaccio 用户呢?不用这么麻烦。根据官方文档和文末的最后一篇文章得知,docker 容器中的 uid 和 gid 和宿主机是共享的,只不过没有具体的名称,而容器内 verdaccio 使用的 uid 为 10001,gid 为 65533,所以我们在宿主机改一下 htpasswd 文件的权限: sudo chown 10001:65533 htpasswd 然后再试一下添加用户,就可以成功了。 同理,storage 目录是 verdaccio 存放包数据的目录,也需要修改一下权限: sudo chown -R 10001:65533 storage

7.代码推送

npm publish --registry your.npm-server

8.npm包太大推送失败

verdaccio Error: 413 Payload Too Large - PUT request entity too large docker npm默认包大小10Mb, 修改verdaccio/config/config.yaml

...
max_body_size: 100mb