安装
Nexus号称是世界上最流行的私服管理软件(The world’s most popular repository),可以搭建几乎目前所有常见的仓库如npm,maven,yum等,Nexus3更是增加了对docker仓库的支持 ,应该是搭建私服的唯一选择.
Nexus有两个版本 Nexus Repository OSS和Nexus Repository Pro,其中OSS版本是免费,Pro专业版需要收费.OSS对于日常的仓库管理已经足够用.
Nexus OSS支持二进制文件安装和docker安装,这里选择docker安装,简单方便.
运行以下命令即可完成安装
$ docker run -d -p 8081:8081 sonatype/nexus3
#可以挂载本地目录做数据持久化
$ mkdir /some/dir/nexus-data && chown -R 757 /some/dir/nexus-data
$ docker run -d -p 8081:8081 -v /some/dir/nexus-data:/nexus-data sonatype/nexus3
在nexus3官方docker仓库说明文档里挂载目录设置的权限为200
但实际安装的时候无法启动,权限不够,错误如下:
mkdir: cannot create directory '../sonatype-work/nexus3': Permission denied
mkdir: cannot create directory '../sonatype-work/nexus3': Permission denied
Java HotSpot(TM) 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to Permission denied
Warning: Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning: Forcing option -XX:LogFile=/tmp/jvm.log
Unable to update instance pid: Unable to create directory /nexus-data/instances
/nexus-data/log/karaf.log (Permission denied)
Unable to update instance pid: Unable to create directory /nexus-data/instances
Exception in thread "Thread-2" java.lang.SecurityException: Could not lock User prefs. Lock file access denied.
at java.util.prefs.FileSystemPreferences.checkLockFile0ErrorCode(FileSystemPreferences.java:937)
at java.util.prefs.FileSystemPreferences.lockFile(FileSystemPreferences.java:925)
at java.util.prefs.FileSystemPreferences.sync(FileSystemPreferences.java:729)
at java.util.prefs.FileSystemPreferences.flush(FileSystemPreferences.java:824)
at java.util.prefs.FileSystemPreferences.syncWorld(FileSystemPreferences.java:464)
at java.util.prefs.FileSystemPreferences.access$1200(FileSystemPreferences.java:50)
at java.util.prefs.FileSystemPreferences$4$1.run(FileSystemPreferences.java:442)
修改为757
后正常启动.等待启动完,访问http://host:8081
测试是否安装成功.
Nextus3搭建docker私有仓库
Nextus3仓库都有三种类型
- hosted(本地类型)资产的提交和拉取都基于本地存储
- proxy(代理类型)本地不做数据存储,可以和hosted配合
- group(组合类型)可以组合多个hosted和proxy并按顺序,并提供统一访问地址
docker本地仓库(hosted)搭建
- 访问
http://host:8081
登陆,默认用户名密码admin/admin123 - 点击
Repository->Blob Stores->Create blob store
创建本地存储
Type:file
Name:definesys-docker-store
Path:/nexus-data/blobs/definesys-docker-store
因为要存储镜像请保证该目录下有足够空间,可以挂在NAS到该目录下.
- 点击
Repositories->Create repositories->docker(hosted)
创建本地仓库
Name(仓库名称):definesys-docker-repo
Http(仓库开放端口):8001
Docker Registry API Support:必须选上
Storage:选择刚创建的definesys-docker-store
其余选项默认即可
这样就完成一个docker私有仓库的创建,过程还是很简单的.
docker代理仓库(proxy)搭建
- 点击
Repositories->Create repositories->docker(proxy)
创建代理仓库
Name(仓库名称):definesys-docker-proxy
Http(仓库开放端口):8002
Docker Registry API Support:必须选上
Proxy->remote storage:https://registry-1.docker.io/
Docker index选择Use Docker Hub
Storage:选择刚创建的definesys-docker-store这样就可以和hosted共用一个存储实现本地优先本地拉取本地找不到再从远程仓库拉取
其余选项默认即可
docker组合仓库(group)搭建
Name(仓库名称):definesys-docker-group
Http(仓库开放端口):8003
Docker Registry API Support:必须选上
Storage:选择刚创建的definesys-docker-store
Group:
definesys-docker-repo
definesys-docker-proxy
其余选项默认即可
docker客户端配置
- 编辑
daemon.json
文件
$ vi /etc/docker/daemon.json
{
"insecure-registries": [
"http://localhost:7001",
"http://localhost:7002",
"http://localhost:7003"
],
"disable-legacy-registry": true
}
- 重启docker
$ systemctl restart docker
- 启动Nexus3容器,将三个端口映射到主机上
$ docker run -d -p 8081:8081 -p 8001:8001 -p 8003:8003 -p 8002:8002 -v /root/u01/nexus-data:/nexus-data sonatype/nexus3
4.分别登陆三个仓库
$ docker login -u admin -p admin123 http://localhost:8001
$ docker login -u admin -p admin123 http://localhost:8002
$ docker login -u admin -p admin123 http://localhost:8003
Nexus会将登陆信息记录在~/.docker/config.json
,这样下次就不用再次登陆:
{
"auths": {
"http://localhost:8003": {
"auth": "YWRtaW46YWRtaW4xMjM="
},
"http://localhost:8001": {
"auth": "YWRtaW46YWRtaW4xMjM="
},
"http://localhost:8002": {
"auth": "YWRtaW46YWRtaW4xMjM="
}
}
}
docker镜像上传
- 将本地镜像打tag(这里以ubuntu镜像为例)上传到hosted仓库(8001)
$ docker tag docker.io/ubuntu:15.10 localhost:8001/defiensys-ubuntu:15.10
$ docker push localhost:8001/defiensys-ubuntu:15.10
- 登陆Nexus3查看结果
docker镜像拉取
- 通过group仓库
#拉取docker hub上镜像
$ docker pull localhost:8003/mysql
#拉取私服镜像
$ docker pull localhost:8003/defiensys-ubuntu:15.10
Nextus3搭建maven私有仓库
待续…
Nextus3搭建npm私有仓库
待续…
遗留问题
- 无法通过group上传镜像,报以下错误
$ docker push localhost:8003/oraclelinux:v1.0
The push refers to a repository [localhost:8003/oraclelinux]
3b431d8f4cd1: Preparing
error parsing HTTP 404 response body: invalid character '<' looking for beginning of value: "\n<!DOCTYPE html>\n<html>\n<head>\n <title>404 - Nexus Repository Manager</title>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n\n\n <!--[if lt IE 9]>\n <script>(new Image).src=\"http://localhost:8003/favicon.ico?3.13.0-01\"</script>\n <![endif]-->\n <link rel=\"icon\" type=\"image/png\" href=\"http://localhost:8003/favicon-32x32.png?3.13.0-01\" sizes=\"32x32\">\n <link rel=\"mask-icon\" href=\"http://localhost:8003/safari-pinned-tab.svg?3.13.0-01\" color=\"#5bbad5\">\n <link rel=\"icon\" type=\"image/png\" href=\"http://localhost:8003/favicon-16x16.png?3.13.0-01\" sizes=\"16x16\">\n <link rel=\"shortcut icon\" href=\"http://localhost:8003/favicon.ico?3.13.0-01\">\n <meta name=\"msapplication-TileImage\" content=\"http://localhost:8003/mstile-144x144.png?3.13.0-01\">\n <meta name=\"msapplication-TileColor\" content=\"#00a300\">\n\n <link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8003/static/css/nexus-content.css?3.13.0-01\"/>\n</head>\n<body>\n<div class=\"nexus-header\">\n <a href=\"http://localhost:8003\">\n <div class=\"product-logo\">\n <img src=\"http://localhost:8003/static/images/nexus.png?3.13.0-01\"/>\n </div>\n <div class=\"product-id\">\n <div class=\"product-id__line-1\">\n <span class=\"product-name\">Nexus Repository Manager</span>\n </div>\n <div class=\"product-id__line-2\">\n <span class=\"product-spec\">OSS 3.13.0-01</span>\n </div>\n </div>\n </a>\n</div>\n\n<div class=\"nexus-body\">\n <div class=\"content-header\">\n <img src=\"http://localhost:8003/static/rapture/resources/icons/x32/exclamation.png?3.13.0-01\"/>\n <span class=\"title\">Error 404</span>\n <span class=\"description\">Not Found</span>\n </div>\n <div class=\"content-body\">\n <div class=\"content-section\">\n Not Found\n </div>\n </div>\n</div>\n</body>\n</html>\n\n"