docker 部署 verdaccio

verdaccio 是一个开源 npm 私有仓库,我为了做实验,自己搭一个玩玩。

安装 verdaccio

获取verdaccio的例子

1
2
3
4
5
git clone -b 5.x https://github.com/verdaccio/verdaccio.git

cp -r verdaccio/docker-examples/docker-local-storage-volume/ ../opt/verdaccio/

cd ../opt/verdaccio/

../opt/verdaccio/ 是我 docker 容器持久化的文件夹。官方的例子和我想要的不太一致,所有需要自己手动修改一下docker-compose.yaml,修改后的内容为:

1
cat docker-compose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: '2.1'
services:
verdaccio:
image: verdaccio/verdaccio:4
container_name: verdaccio
restart: always
ports:
- '14873:4873'
volumes:
- './storage:/verdaccio/storage'
- './conf:/verdaccio/conf'
volumes:
verdaccio:
driver: local

因为容器的权限和本机权限会不一致,需要对容器的文件夹更改权限。

1
2
#../opt/verdaccio/
sudo chown -R 10001:65533 .

后台部署:

1
docker-compose up -d

安装配置 nrm

nrm 是 npm registry 管理器,能方便的切换源。安装很简单:

1
npm i -g nrm

查看当前使用的 registry:

1
nrm ls

添加一个新的 registry:

1
nrm add verdaccio http://10.0.0.3:14873/

使用一个新的 registry:

1
nrm use verdaccio

删除一个 registry:

1
nrm del verdaccio

使用

publish 已有包

我本来有一个包max6675-raspi两年前就发布到npmjs.com上了,因为verdaccio内部有个逻辑是:在推送时,只要远程仓库 (npmjs.com) 存在此包,则不能推送同一个版本到仓库。很多人解决这个问题的方案就是修改package.jsonversion或者name,使其升级或者不同名。

这不是我想要的结果呀,只能把代理先屏蔽掉:

1
sudo vim conf/config.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# 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://github.com/verdaccio/verdaccio/blob/master/wiki/docker.md#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

auth:
htpasswd:
file: /verdaccio/conf/htpasswd
# Maximum amount of users allowed to register, defaults to "+inf".
# You can set this to -1 to disable registration.
#max_users: 1000
security:
api:
jwt:
sign:
expiresIn: 60d
notBefore: 1
web:
sign:
expiresIn: 7d

# a list of other known repositories we can talk to
uplinks:
#npmjs:
# url: https://registry.npmjs.org/
#cnpm:
# url: http://r.cnpmjs.org/

packages:
'@jota/*':
access: $all
publish: $all

'@*/*':
# scoped packages
access: $all
publish: $all
#proxy: cnpm

'**':
# 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 packages
# (anyone can register by default, remember?)
publish: $all

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

# To use `npm audit` uncomment the following section
middlewares:
audit:
enabled: true

# publish config
## Special packages publish configurations
publish:
## This will allow the publisher to publish packages even if any uplink is down.
allow_offline: true

# log settings
logs:
- { type: stdout, format: pretty, level: trace }
#- {type: file, path: verdaccio.log, level: info}

重启verdaccio

1
docker-compose restart verdaccio

这样子再把代码推上去

1
npm publish --registry http://10.0.0.3:14873

推上去之后再打开配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# 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://github.com/verdaccio/verdaccio/blob/master/wiki/docker.md#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

auth:
htpasswd:
file: /verdaccio/conf/htpasswd
# Maximum amount of users allowed to register, defaults to "+inf".
# You can set this to -1 to disable registration.
#max_users: 1000
security:
api:
jwt:
sign:
expiresIn: 60d
notBefore: 1
web:
sign:
expiresIn: 7d

# a list of other known repositories we can talk to
uplinks:
#npmjs:
# url: https://registry.npmjs.org/
cnpm:
url: http://r.cnpmjs.org/

packages:
'@jota/*':
access: $all
publish: $all

'@*/*':
# scoped packages
access: $all
publish: $all
proxy: cnpm

'**':
# 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 packages
# (anyone can register by default, remember?)
publish: $all

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

# To use `npm audit` uncomment the following section
middlewares:
audit:
enabled: true

# publish config
## Special packages publish configurations 重启`verdaccio`
publish:
## This will allow the publisher to publish packages even if any uplink is down.
allow_offline: true

# log settings
logs:
- { type: stdout, format: pretty, level: trace }
#- {type: file, path: verdaccio.log, level: info}

重启verdaccio

1
docker-compose restart verdaccio

缓存

这个配置里面,默认verdaccio是缓存下载过的公共包的,也就是说我们可以使用这个来加速安装包,这也是目前这个私有仓库最大的优势。verdaccio 还不能管理上传的包,功能还是很少,希望以后能有更多的功能吧。


docker 部署 verdaccio
https://bubao.github.io/posts/efe03d49.html
作者
一念
发布于
2021年1月21日
许可协议