使用nexus3配置docker私有仓库
# 2,重启 docker。
$systemctl daemon-reload
$systemctl restart docker
2
# 3,pull 镜像。
docker pull redis
# 4,登陆私服。
docker login -u admin -p admin123 192.168.157.110:8082
# 5,打标签。
docker tag docker.io/redis 192.168.157.110:8082/redis
# 6,push 镜像。
[root@docker ~]$docker push 192.168.157.110:8082/redis
然后报错:
The push refers to a repository [192.168.157.110:8082/redis]
902afb26cfff: Layer already exists
21497520b817: Layer already exists
a3514b4102be: Layer already exists
714e32c05337: Layer already exists
d98fb630fb3b: Layer already exists
8b15606a9e3e: Layer already exists
error parsing HTTP 404 response body: unexpected end of JSON input: ""
2
3
4
5
6
7
8
9
10
11
12
暂时不知这个报错问题的原因是什么,因为最终没有采用这种方式,所以没有深入探究。
# 5,nginx 代理方式。
以下内容参考张戈博客,中有删改。
在部署 Nginx 部分,我们先需要生成自签名 SSL 证书,因为后面不想在 docker pull 的时候还要带一个端口!这里我们需要 2 个域名,一个用来展示 nexus 前台,另一个用做 docker 仓库,比如:
-
nexus 前台:
repo.ald.com
-
docker 仓库:
idocker.io
# 1,安装 nginx。
先通过
curl 192.168.106.10/a | sh
安装 nginx。
# 2,生成证书。
生成自签名 SSL 证书的方法网上很多,这里推荐一个一键生成工具,大家可以尝试使用:
- name: ssl
desc: ssl生成工具
avatar: https://avatars2.githubusercontent.com/u/416130?s=460&u=8753e86600e300a9811cdc539aa158deec2e2724&v=4 # 可选
link: https://github.com/Fishdrowned/ssl # 可选
bgColor: "#0074ff" # 可选,默认var(--bodyBg)。颜色值有#号时请添加单引号
textColor: "#fff" # 可选,默认var(--textColor)
2
3
4
5
6
这个工具。
创建方式如下:
#直接切换到应用目录。
[root@nexus ~]$cd /usr/local/nginx/conf
#下载工具。
[root@nexus conf]$git clone https://github.com/Fishdrowned/ssl.git
Cloning into 'ssl'...
remote: Enumerating objects: 106, done.
remote: Total 106 (delta 0), reused 0 (delta 0), pack-reused 106
Receiving objects: 100% (106/106), 171.53 KiB | 286.00 KiB/s, done.
Resolving deltas: 100% (48/48), done.
#生成证书。
[root@nexus ssl]$./gen.cert.sh idocker.io
Using configuration from ./ca.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :ASN.1 12:'Guangdong'
localityName :ASN.1 12:'Guangzhou'
organizationName :ASN.1 12:'Fishdrowned'
organizationalUnitName:ASN.1 12:'idocker.io'
commonName :ASN.1 12:'*.idocker.io'
Certificate is to be certified until Oct 16 06:18:13 2022 GMT (1461 days)
Write out database with 1 new entries
Data Base Updated
Certificates are located in:
lrwxrwxrwx 1 root root 37 Oct 16 14:18 /usr/local/nginx/conf/ssl/out/idocker.io/idocker.io.bundle.crt -> ./20181016-1418/idocker.io.bundle.crt
lrwxrwxrwx 1 root root 30 Oct 16 14:18 /usr/local/nginx/conf/ssl/out/idocker.io/idocker.io.crt -> ./20181016-1418/idocker.io.crt
lrwxrwxrwx 1 root root 15 Oct 16 14:18 /usr/local/nginx/conf/ssl/out/idocker.io/idocker.io.key.pem -> ../cert.key.pem
lrwxrwxrwx 1 root root 11 Oct 16 14:18 /usr/local/nginx/conf/ssl/out/idocker.io/root.crt -> ../root.crt
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
# 3,配置 nginx。
将如下配置写入
nginx.conf
。
upstream nexus_docker_get {
server 192.168.157.110:8082;
upstream nexus_docker_put {
server 192.168.157.110:8083;
server {
listen 80;
listen 443 ssl;
server_name idocker.io;
access_log /var/log/nginx/idocker.io.log;
ssl_certificate /usr/local/nginx/conf/ssl/out/idocker.io/idocker.io.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/out/cert.key.pem;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers '!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES:';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
# disable any limits to avoid HTTP 413 for large image uploads
client_max_body_size 0;
# required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
chunked_transfer_encoding on;
# 设置默认使用推送代理
set $upstream "nexus_docker_put";
# 当请求是GET,也就是拉取镜像的时候,这里改为拉取代理,如此便解决了拉取和推送的端口统一
if ( $request_method ~* 'GET') {
set $upstream "nexus_docker_get";
index index.html index.htm index.php;
location / {
proxy_pass http://$upstream;
proxy_set_header Host $host;
proxy_connect_timeout 3600;
proxy_send_timeout 3600;
proxy_read_timeout 3600;
proxy_set_header X-Real-IP $remote_addr;
proxy_buffering off;
proxy_request_buffering off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
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
如果有一些操作与本文不一致,那么请根据自己实际情况进行修改。
nginx -t
检查没有问题的话,就可以启动 nginx 了。
# 4,客户端配置。
部署完成之后,我们就可以找一台测试机器进行测试了,不过因为我们刚刚定义的是内部使用的域名,所以需要在测试机器上写 hosts 解析,并将证书拷贝过去,否则会报报不信任的错误。
在上文介绍的一键生成自签名工具中,会生成一个根证书,名称为
/usr/local/nginx/conf/ssl/out/root.crt
,我们将这个文件上传到客户端服务器的
/etc/docker/certs.d/idocker.io
目录即可(注意目录需要创建,最后的文件夹名称和仓库域名保持一致:
idocker.io
)。
现在到一台新主机上测试:
[root@docker ~]$echo "192.168.157.110 idocker.io" >> /etc/hosts
[root@docker ~]$mkdir -p /etc/docker/certs.d/idocker.io
然后去nexus主机上,将刚才的证书拷过来:
scp /usr/local/nginx/conf/ssl/out/root.crt [email protected]:/etc/docker/certs.d/idocker.io/ca.crt
2
3
4
5
6
接下来,就可以开始真正的使用了。
# 6,正式验证。
# 1,pull 镜像。
[root@docker ~]$docker pull docker.io/nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
f17d81b4b692: Pull complete
d5c237920c39: Pull complete
a381f92f36de: Pull complete
Digest: sha256:4ddaf6043a77aa145ce043d4c662e3768556421d6c0a65d303e89977ad3c9636
Status: Downloaded newer image for docker.io/nginx:latest
[root@docker ~]$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest dbfc48660aeb 45 minutes ago 109 MB
2
3
4
5
6
7
8
9
10
11
12
13
# 2,登陆私服。
这个地方也可能在登陆的时候会报错,说证书过期什么的,如下:
[root@moban idocker.io]$docker login -u admin -p admin123 idocker.io
Error response from daemon: Get https://idocker.io/v1/users/: x509: certificate has expired or is not yet valid
2
报这个错的情况下,大概原因只有一个,那就是,两台服务器的时间不一致,只需要将两台服务器时间保持一致即可。
yum -y install ntpdate && ntpdate -u cn.pool.ntp.org
分别在两台主机执行之后,发现登陆就成功了。
[root@docker ~]$docker login -u admin -p admin123 idocker.ioLogin Succeeded
# 3,打标签。
[root@docker ~]$docker tag docker.io/nginx idocker.io/nginx
[root@docker ~]$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest dbfc48660aeb 49 minutes ago 109 MB
idocker.io/nginx latest dbfc48660aeb 49 minutes ago 109 MB
2
3
4
5
6
# 4,push 镜像。
[root@docker ~]$docker push idocker.io/nginx
The push refers to a repository [idocker.io/nginx]
86df2a1b653b: Pushed
bc5b41ec0cfa: Pushed
237472299760: Pushed
latest: digest: sha256:d98b66402922eccdbee49ef093edb2d2c5001637bd291ae0a8cd21bb4c36bebe size: 948
2
3
4
5
6
这里上传成功了,再去 nexus3 里边看看是有上去了。
# 5,测试从私服拉镜像。
[root@docker ~]$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker ~]$docker pull idocker.io/nginx
Using default tag: latest
Trying to pull repository idocker.io/nginx ...
latest: Pulling from idocker.io/nginx
f17d81b4b692: Pull complete
d5c237920c39: Pull complete
a381f92f36de: Pull complete
Digest: sha256:d98b66402922eccdbee49ef093edb2d2c5001637bd291ae0a8cd21bb4c36bebe
Status: Downloaded newer image for idocker.io/nginx:latest
[root@docker ~]$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
idocker.io/nginx latest dbfc48660aeb 59 minutes ago 109 MB
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 7,代理的功能展示。
当某一个镜像在我们本地仓库没有的时候,就需要从远程仓库拉取了,其他的私有仓库的操作大概都是要从远程拉取,然后在重复如上操作推到本地私有仓库,而 nexus 因为有了 proxy 功能,因此,当我们在 pull 远程镜像的时候,本地就会自动同步下来了,这,就是其核心竞争力,也是与其他几款产品本质上的区别。
下边,我以拉取 gitlab 镜像为例:
[root@docker ~]$docker pull idocker.io/gitlab/gitlab-ce
Using default tag: latest
Trying to pull repository docker.io/gitlab/gitlab-ce ...
latest: Pulling from docker.io/gitlab/gitlab-ce
3b37166ec614: Pull complete
504facff238f: Pull complete
ebbcacd28e10: Pull complete
c7fb3351ecad: Pull complete
2e3debadcbf7: Pull complete
8e5e9b12009c: Pull complete
0720fffe6e22: Pull complete
2f336a213238: Pull complete
1656ee3e1127: Pull complete
25fa5248fd38: Pull complete
36b8c1d869a0: Pull complete
Digest: sha256:0dd22880358959d9a9233163147adc4c8f1f5d5af90097ff8dfa383c6be7e25a
Status: Downloaded newer image for docker.io/gitlab/gitlab-ce:latest
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
因为本地没有这个镜像,所以从远程仓库拉取,然后去仓库里看看啥情况:
这里可以看到,在我们创建的 proxy 分组中有一个 library 目录,已经自动缓存下来了这个镜像。
接着去我们用的 group 里边看看。
我们看到 group 里边也已经有了 gitlab 这个镜像,如果以后再有人拉取 gitlab 这个镜像,直接就可以从本地私服进行拉取了,而不用再到公网去下了,事实上,使用 nexus3 作为 maven 的私服其实也是这样一个原理,从而实现节约带宽,节约时间,等等优势。
至此,基本上关于使用 nexus3 搭建 docker 私有仓库的知识点,基本上已经知无不言,言无不尽的分享完毕了。
# 8,参考地址。
感恩前行者的付出,让我们能够走得如此顺畅,感谢自己认真钻研,从而能够把好东西用更好的方式分享出去!
-
https://zhangge.net/5139.html
-
https://segmentfault.com/a/1190000015629878#articleHeader5