Docker 安装Nexus3 仓库
1.安装
镜像仓库有多种,目前常用的是Harbo或Nexus,由于Nexus支持范围更广,更适合项目开发团队使用,我们将选择使用Nexus建立内部仓库。
由于我们前面安装了K8S环境,Nexus仓库我们可以使用K8S来安装及管理,也可以直接使用Docker安装,通常建议仓库独立于K8S环境安装,我们采用Docker直接部署。
首先在服务器上创建目录/data/nexus/nexus-data,用于存储镜像数据。
chmod 777 /data/nexus/nexus-data #设置目录权限
docker search nexus #查询镜像
docker run -d --name nexus3 \
--restart=always \
-p 8081:8081 \
-p 8082:8082 \
-p 8083:8083 \
-p 8084:8084 \
-p 8085:8085 \
-v /data/nexus/nexus-data:/nexus-data \
sonatype/nexus3
2.配置
安装完成后,我们使用http://ip:8081访问nexus。

点击右上角登录,提示password需要去/nexus-data/admin.password文件中找

使用下面的命令查看初始密码,注意路径,也可以使用docker exec -it 进容器中查看
cat /data/nexus/nexus-data/admin.password #就可以看到初始密码了
使用该密码登录后,我们开始初始化仓库
新建Docker Host仓库

类型选择docker(hosted),端口8083


新建Docker Proxy仓库,端口8084
docker proxy是指代理远程的仓库,通常是代理公开镜像仓库,可以建多个源,比如163/阿里,相当于建立一个传输通道。

新建Docker Group仓库,端口8082
Docker Group是一个仓库聚合,我们对外暴露仓库通常暴露该聚合仓库地址。
聚合模式通常是本地Host仓库+Proxy远程仓库,如下,我们聚合了163和我们本地的仓库(优先本地),可以通过上下箭头调整优先级,上面的优先级高。

创建完成后,我们可以mydocker-group的地址为
http://ip:8081/repository/docker-group

Realms设置

3.使用
修改docker的daemon仓库地址
vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://ip:8081"],
"insecure-registries": ["ip:8081"],
"graph": "/data/docker/path"
}
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
重启Docker
systemctl daemon-reload
systemctl restart docker
测试是否可用
[root@k8s-node2 nexus-data]# docker login http://192.168.0.230:8082
Username: admin
Password:
Error response from daemon: Get https://192.168.0.230:8082/v2/: http: server gave HTTP response to HTTPS client
这个错误是因为我们使用的是http协议,而docker获取镜像默认走的是https,如果需要走http,则需要加入到例外清单中,方法如下:
vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.0.230:8082","192.168.0.230:8083","192.168.0.230:8084"],
"registry-mirrors": ["http://192.168.0.230:8082"],
"graph": "/data/docker/path"
}
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
重启容器:
重启
[root@k8s-node2 ~]# systemctl daemon-reload
[root@k8s-node2 ~]# sudo service docker restart
再次尝试登录
docker login http://192.168.0.230:8082
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
登录成功
push一个镜像测试下
#先打标签,nginx我们之前已经下载过,如果没有,则docker pull nginx下载
docker tag nginx 192.168.0.230:8082/nginx01:v1
#推送到镜像仓库
docker push 192.168.0.230:8082/nginx01:v1
返回404错误: error parsing HTTP 404 response body: invalid character
原因是我们使用8082是docker-group的端口
我们改为8083端口(docker-host)重新尝试
#先登录下8083端口
[root@k8s-node2 nexus-data]# docker login http://192.168.0.230:8083
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
#重新打一个标签
[root@k8s-node2 nexus-data]# docker tag nginx 192.168.0.230:8083/nginx01:v1
#推送
[root@k8s-node2 nexus-data]# docker push 192.168.0.230:8083/nginx01:v1
The push refers to repository [192.168.0.230:8083/nginx01]
55a77731ed26: Pushed
71f2244bc14d: Pushed
f2cb0ecef392: Pushed
v1: digest: sha256:3936fb3946790d711a68c58be93628e43cbca72439079e16d154b5db216b58da size: 948
[root@k8s-node2 nexus-data]#
提示上传成功,如下图所示,我们已经可以在我们自己的仓库中看到该镜像。

使用我们就可以直接内网pull:
docker pull 192.168.0.230:8083/nginx01:v1
v1: Pulling from nginx01
68ced04f60ab: Pull complete
28252775b295: Pull complete
a616aa3b0bf2: Pull complete
Digest: sha256:3936fb3946790d711a68c58be93628e43cbca72439079e16d154b5db216b58da
Status: Downloaded newer image for 192.168.0.230:8083/nginx01:v1
4.其他
Nexus支持的源很多,YUM源,Maven源,Nuget源均可以配置出来;另外可以配合Nginx或Kong映射出不同的域名:docker.xxxx.com/maven.xxx.com/nuget.xxx.com;nexus.xxx.com