一、说明
用nexus搭建docker私有镜像仓库,我们可以去官网下载nexus安装包安装,然后做安装配置。
【nexus】用nexus3.5搭建docker私有仓库
https://www.jianshu.com/p/7a7db54a538f
从nexus3.x开始,我们的另一个选择是拉nexus的镜像,用容器运行nexus服务。
![](http://img.e-com-net.com/image/info10/e262a52702e743d2901cca2cceb4e957.jpg)
二、实验环境
操作系统: CentOS7.5 Minimal
nexusServer 192.168.1.106
dockerClient 192.168.1.104
三、 安装docker
在nexusServer 和dockerClient 服务器
关闭selinux
# setenforce 0
# sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
安装docker
# yum -y install yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum list docker-ce --showduplicates| sort -r
![](http://img.e-com-net.com/image/info10/1b2fadfb1f684c8b9d8c6fba67791ee4.jpg)
# yum -y install docker-ce-18.06.0.ce
# systemctl start docker
# systemctl status docker
# systemctl enable docker
# docker version
![](http://img.e-com-net.com/image/info10/dd31ea26263b43c08e620479fa85b945.jpg)
![](http://img.e-com-net.com/image/info10/476fe0fa16454740aa643b239d25a002.jpg)
四、拉取镜像,运行nexus服务
在nexusServer 服务器
# docker pull sonatype/nexus3:3.16.0
# docker images
![](http://img.e-com-net.com/image/info10/3a7073d9460746d38a4dd4cbb8482bb2.jpg)
# mkdir /opt/nexus-data
# chown -R 200 /opt/nexus-data
注:容器中nexus的默认运行用户是nexus,uid和gid为200
# docker run -it --rm sonatype/nexus3:3.5.2 cat /etc/passwd
![](http://img.e-com-net.com/image/info10/6eede6fce8684710ade764967b4a9c04.jpg)
为什么需要提前创建目录并更改属主属组呢?
因为容器中nexus进程是普通用户nexus启动的,不是root,普通用户无法再宿主机上创建目录,如果目录属主不是nexus用户(或者映射在宿主的用户id),那么这个进程就没有写入权限。
用命令行形式运行nexus容器
# docker run -d \
--restart=always \
--name nexus \
--ulimit nofile=65536:65536 \
-p 192.168.1.106:8081:8081 \
-v /opt/nexus-data:/nexus-data \
sonatype/nexus3:3.16.0
![](http://img.e-com-net.com/image/info10/03bb9c4ab6fa48ea8add2a670dfd849a.png)
# docker logs -f nexus
![](http://img.e-com-net.com/image/info10/afba86c0408a496e842bf9e194aa8147.jpg)
# docker ps -a
# ss -tan
![](http://img.e-com-net.com/image/info10/0f2b788900a04ece9e621b6154202368.jpg)
浏览器访问: http:192.168.1.106:8081
![](http://img.e-com-net.com/image/info10/758ccab5b4f545b79c3d4330376788b5.jpg)
![](http://img.e-com-net.com/image/info10/7053617a22c241a7a3acda30c0757348.jpg)
五,创建一个docker仓库
浏览器访问: http:192.168.1.106:8081
默认登录用户密码:admin/admin123
![](http://img.e-com-net.com/image/info10/3777f0988b604662a875ee73c8a7ae26.jpg)
![](http://img.e-com-net.com/image/info10/221f06302ae444e4981d80b5d06c9487.jpg)
![](http://img.e-com-net.com/image/info10/ccc7e7b07c1644a190c927f4db953f3b.jpg)
![](http://img.e-com-net.com/image/info10/4566e82cc9c64264bacf585dd6ad5406.jpg)
官方镜像搭建的nexus,不支持https,仓库端口只能选择http,否则服务异常
![](http://img.e-com-net.com/image/info10/e0a45e9804fa4cacbb99f65994a3304c.jpg)
![](http://img.e-com-net.com/image/info10/9b288e7303ad44faa1f2b326b5efba81.jpg)
我们创建了一个名为 test的镜像仓库,仓库端口为 2019,协议为http,不是https!
重启nexus服务,开放2019端口
# docker stop nexus
# docker rm nexus
# docker run -d \
--restart=always \
--name nexus \
--ulimit nofile=65536:65536 \
-p 192.168.1.106:8081:8081 \
-p 192.168.1.106:2019:2019 \
-v /opt/nexus-data:/nexus-data \
sonatype/nexus3:3.16.0
![](http://img.e-com-net.com/image/info10/dfbfc9444f314c57a7ff3cfc17b65ec8.png)
# docker ps -a
# ss -tan
![](http://img.e-com-net.com/image/info10/1ce90335a84845ec8f67f0ae6024fc55.jpg)
服务端启动方式改进,将nexus注册成系统服务
编写unit文件
# vim /etc/systemd/system/nexus.service
####################################################
[Unit]
Description=Nexus
Documentation=https://www.sonatype.com
After=network-online.target docker.service
Requires=docker.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f nexus
ExecStart=/usr/bin/docker run \
--name nexus \
--ulimit nofile=65536:65536 \
-p 192.168.1.106:8081:8081 \
-p 192.168.1.106:2019:2019 \
-v /opt/nexus-data:/nexus-data \
sonatype/nexus3:3.16.0
ExecStop=/usr/bin/docker stop nexus
LimitNOFILE=65535
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
#####################################################
![](http://img.e-com-net.com/image/info10/b505a8a6b5dc4af3b3611bf62aaecf92.jpg)
停止和删除命令行启动的nexus服务
# docker stop nexus
# docker rm nexus
用systemd启动服务
# systemctl daemon-reload
# systemctl start nexus
# systemctl enable nexus
# systemctl status nexus
![](http://img.e-com-net.com/image/info10/abe666ab29314a8ba8daaf2718960ffa.jpg)
六、客户端测试
测试服务端端口连通性
# echo > /dev/tcp/192.168.1.106/8081
# echo > /dev/tcp/192.168.1.106/2019
![](http://img.e-com-net.com/image/info10/f0993b7428ba4d79afa34acf2f03727f.png)
# curl -I http://192.168.1.106:8081
# curl -I http://192.168.1.106:2019
![](http://img.e-com-net.com/image/info10/cec21dfc5a764b84934ff97449824a73.jpg)
在nexusClient客户端登录仓仓库
# docker login http://192.68.1.106:2019 -u admin -p "admin123"
# cat /root/.docker/config.json
![](http://img.e-com-net.com/image/info10/c04412291c884bbf9382937a9e3d9d86.jpg)
nexsu仓库开的是http,dockr 要走https,怎么解决?
添加仓库信任
# vim /usr/lib/systemd/system/docker.service
#######################################################
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.106:2019
########################################################
![](http://img.e-com-net.com/image/info10/00f59cea4983474ba2674d50e3173530.jpg)
systemctl daemon-reload
# systemctl restart docker
# docker login 192.168.1.106:2019 -u admin -p "admin123"
# docker login http://192.168.1.106:2019 -u admin -p "admin123"
![](http://img.e-com-net.com/image/info10/0d3a0a79f90e4375910cdd541dbe99be.jpg)
测试推送一个镜像
# docker pull busybox:latest
# docker tag busybox:latest 192.168.1.106:2019/busybox:v1
# docker push 192.168.1.106:2019/busybox:v1
![](http://img.e-com-net.com/image/info10/19d2c08cf9c74bef8133ffda4a3e52d7.jpg)
![](http://img.e-com-net.com/image/info10/a69e3f67eae14ea9b887921c4f5317bb.jpg)
七、改nexus仓库的http为https
前面我们用docker容器搭建nexus服务,创建了一个名为 test的镜像仓库,仓库端口为 2019,协议为http,不是https。
nexsu仓库开的是http,dockr 要走https,我们是通过在客户端添加仓库信任解决的。
那么,有没有更符合最佳实践的方式呢?有!用nexus-https镜像,nexus官方镜像的改进版。
Sonatype Nexus Repository Manager 3 with HTTPS support, based on CentOS
bradbeck/nexus-https
https://hub.docker.com/r/bradbeck/nexus-https
https://github.com/bradbeck/nexus-https
在nexusServer 服务器
# docker stop nexus
# docker rm nexus
# rm -rf /opt/nexus-data/*
# docker pull bradbeck/nexus-https
# docker images
![](http://img.e-com-net.com/image/info10/440c383ad351409e99096b2e4ffcb3db.jpg)
用nexus-https镜像起一个容器,获取配置https所需的配置文件
# docker run -it --name nexus-https --rm bradbeck/nexus-https:latest bash
![](http://img.e-com-net.com/image/info10/011cb0f63e304fdb9d0093dd03b45db1.jpg)
对nexusServer服务器,另开一个Xshell窗口
# docker ps -a
![](http://img.e-com-net.com/image/info10/d8f884c9eb4740f2a12dadbe71c24051.jpg)
可以看到,起了一容器ID为 2f3bbae29dd3 的容器,当然,你起的容器ID肯定不同,灵活应变。
从容器中拷贝文件
# docker cp 2f3bbae29dd3:/opt/sonatype/nexus/etc/jetty/jetty-https.xml ./
或者你可以使用一行式:
# docker exec -it nexus-https cat /opt/sonatype/nexus/etc/jetty/jetty-https.xml > jetty-https.xml
![](http://img.e-com-net.com/image/info10/5499e9673bfe4c3fb393072136a74fee.jpg)
创建容器服务相关目录
# mkdir /opt/nexus-data
# mkdir /opt/nexus-ssl
# mkdir /opt/nexus-jetty
![](http://img.e-com-net.com/image/info10/8f42f790a02c4b7081786a8631887ddd.png)
# chown -R 200 /opt/nexus-data /opt/nexus-ssl /opt/nexus-jetty
# cp jetty-https.xml /opt/nexus-jetty
生成keystore证书文件
安装keytool证书工具
# yum -y install java
![](http://img.e-com-net.com/image/info10/67447a981c4a4c5090e06a5823489fe9.jpg)
生成证书
# keytool \
-genkeypair \
-keystore /opt/nexus-ssl/keystore.jks \
-alias nexus \
-keypass nexus@123 \
-storepass nexus@123 \
-keyalg RSA \
-keysize 2048 \
-validity 5000 \
-dname "CN=*.test.com,OU=TEST,O=TEST,L=Shenzhen,ST=Guangdong,C=CN" \
-ext "SAN=IP:192.168.1.106" \
-ext "BC=ca:true"
![](http://img.e-com-net.com/image/info10/4857632cfa434892a1f1ca5e180dd447.jpg)
# ll /opt/nexus-ssl/
![](http://img.e-com-net.com/image/info10/75670008ede74629aa5510fc64a65870.jpg)
# keytool -list -v -storepass "nexus@123" -keystore /opt/nexus-ssl/keystore.jks
![](http://img.e-com-net.com/image/info10/3fb224a1db694f4cbdc66be2d05a92a4.jpg)
修改配置文件中证书默认密码
# sed -i 's/password/nexus@123/g' /opt/nexus-jetty/jetty-https.xml
![](http://img.e-com-net.com/image/info10/8109c050513d4d59a64f7994a247e609.jpg)
用nexus-https镜像启动nexus容器
# docker run -d \
--restart=always \
--name nexus \
--ulimit nofile=65536:65536 \
-p 192.168.1.106:8081:8081 \
-p 192.168.1.106:8443:8443 \
-v /opt/nexus-data:/nexus-data \
-v /opt/nexus-ssl:/opt/sonatype/nexus/etc/ssl/ \
-v /opt/nexus-jetty/jetty-https.xml:/opt/sonatype/nexus/etc/jetty/jetty-https.xml \
bradbeck/nexus-https:latest
![](http://img.e-com-net.com/image/info10/d33532fb89b24127bc3167b7dbbf6a46.jpg)
# docker ps -a
# ss -tan
![](http://img.e-com-net.com/image/info10/9f6e3a067e924485b01f3440835b34ce.jpg)
浏览器访问
https:192.168.1.106:8443
http://192.168.1.106:8081
如果不用http,那么启动容器的时候,不映射http的8081端口到宿主机。
默认登录用户密码:admin/admin123
![](http://img.e-com-net.com/image/info10/ddf7093e6594413592683cee7a9c467f.jpg)
![](http://img.e-com-net.com/image/info10/77beae1a72e4482598013f9b71eb13ab.jpg)
![](http://img.e-com-net.com/image/info10/8fd4daedbdd94bcb892261c8774aef14.jpg)
![](http://img.e-com-net.com/image/info10/2c7ca0acea1f4fa1b87e927c86dfbc78.jpg)
![](http://img.e-com-net.com/image/info10/45453cc88d684e328f6bb70ce63a6e00.jpg)
我们创建了一个名为 test的镜像仓库,仓库端口为 2019,协议为https,不是http!
重启nexus服务,开放2019端口
# docker stop nexus
# docker rm nexus
# docker run -d \
--restart=always \
--name nexus \
--ulimit nofile=65536:65536 \
-p 192.168.1.106:8443:8443 \
-p 192.168.1.106:2019:2019 \
-v /opt/nexus-data:/nexus-data \
-v /opt/nexus-ssl:/opt/sonatype/nexus/etc/ssl/ \
-v /opt/nexus-jetty/jetty-https.xml:/opt/sonatype/nexus/etc/jetty/jetty-https.xml \
bradbeck/nexus-https:latest
![](http://img.e-com-net.com/image/info10/9d9cb5ba8792401b94ce6c4a07e29b7d.jpg)
# docker stop nexus
# docker rm nexus
![](http://img.e-com-net.com/image/info10/92be159a9b2e4587b27a32892f63dcfc.jpg)
服务端启动方式改进,将nexus注册成系统服务
编写unit文件
# vim /etc/systemd/system/nexus.service
####################################################
[Unit]
Description=Nexus
Documentation=https://www.sonatype.com
After=network-online.target docker.service
Requires=docker.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f nexus
ExecStart=/usr/bin/docker run \
--name nexus \
--ulimit nofile=65536:65536 \
-p 192.168.1.106:8443:8443 \
-p 192.168.1.106:2019:2019 \
-v /opt/nexus-data:/nexus-data \
-v /opt/nexus-ssl:/opt/sonatype/nexus/etc/ssl/ \
-v /opt/nexus-jetty/jetty-https.xml:/opt/sonatype/nexus/etc/jetty/jetty-https.xml \
bradbeck/nexus-https:latest
ExecStop=/usr/bin/docker stop nexus
LimitNOFILE=65535
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
#####################################################
停止和删除命令行启动的nexus服务
# docker stop nexus
# docker rm nexus
用systemd启动服务
# systemctl daemon-reload
# systemctl start nexus
# systemctl enable nexus
# docker logs -f nexus
# systemctl status nexus
![](http://img.e-com-net.com/image/info10/c7fd8d0a8e8b479ebd4d0e348e88de25.jpg)
八、客户端测试
在dockerClient服务器
测试服务端端口连通性
# echo > /dev/tcp/192.168.1.106/8443
# echo > /dev/tcp/192.168.1.106/2019
![](http://img.e-com-net.com/image/info10/a58cfc6a2d224b289def2a5d447a1d70.jpg)
# curl -I -k https://192.168.1.106:8443
# curl -I -k https://192.168.1.106:2019
![](http://img.e-com-net.com/image/info10/0a548411b3584f1780c8e94c892d3cd2.jpg)
在nexusClient客户端登录仓库
# docker login 192.168.1.106:2019 -u admin -p "admin123"
![](http://img.e-com-net.com/image/info10/e200b700a4214552b160d97153315918.jpg)
获取nexus服务端证书
# yum -y install java
# keytool -printcert -sslserver 192.168.1.106:2019 -v
# keytool -printcert -sslserver 192.168.1.106:2019 -rfc
![](http://img.e-com-net.com/image/info10/9b262d8ddb9f4c8fa33c5c47308ee99c.jpg)
# keytool -printcert -sslserver 192.168.1.106:2019 -rfc > /etc/pki/ca-trust/source/anchors/nexus.crt
# cat /etc/pki/ca-trust/source/anchors/nexus.crt
![](http://img.e-com-net.com/image/info10/37b5c6a7d0da404d973cf45547fed9b0.jpg)
刷新操作系统认证,重启docker
# update-ca-trust
# systemctl restart docker
# docker login 192.168.1.106:2019 -u admin -p "admin123"
# docker login https://192.168.1.106:2019 -u admin -p "admin123"
# cat /root/.docker/config.json
![](http://img.e-com-net.com/image/info10/620b5e489c7347c2bd9e354dcfa23ed9.jpg)
测试推送一个镜像
# docker pull busybox:latest
# docker tag busybox:latest 192.168.1.106:2019/busybox:v1
# docker push 192.168.1.106:2019/busybox:v1
![](http://img.e-com-net.com/image/info10/0e5491111eb54cd48a795b7392e04ad1.jpg)
![](http://img.e-com-net.com/image/info10/6e07309bd16545e5887b9679cda11015.jpg)
九、参考
sonatype/docker-nexus3
https://hub.docker.com/r/sonatype/docker-nexus3
https://github.com/sonatype/docker-nexus3
nexus3.x docker镜像仓库及仓库代理配置
https://segmentfault.com/a/1190000015629878
sonatype nexus docker volume error
https://stackoverflow.com/questions/36405434/sonatype-nexus-docker-volume-error
Docker — 从入门到实践
https://yeasy.gitbooks.io/docker_practice
Understanding how uid and gid work in Docker containers
https://medium.com/@mccode/understanding-how-uid-and-gid-work-in-docker-containers-c37a01d01cf
bradbeck/nexus-https
https://hub.docker.com/r/bradbeck/nexus-https
https://github.com/bradbeck/nexus-https
Using Self-Signed Certificates with Nexus Repository Manager and Docker Daemon
https://support.sonatype.com/hc/en-us/articles/217542177-Using-Self-Signed-Certificates-with-Nexus-Repository-Manager-and-Docker-Daemon
Transport Layer Security (TLS) Self-Signed Certificates
https://support.sonatype.com/hc/en-us/articles/213465768-SSL-Certificate-Guide
Nexus Repository Manager 3 using SSL Unreachable by browsers or Docker
https://stackoverflow.com/questions/53183851/nexus-repository-manager-3-using-ssl-unreachable-by-browsers-or-docker