二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • Nexus系列文章

    • 前言
    • nexus的安装
    • 使用nexus3配置docker私有仓库
      • 1,安装 nexus3。
      • 2,配置走起。
        • 1,创建 blob 存储。
        • 2,创建一个 hosted 类型的 docker 仓库。
        • 3,创建一个 proxy 类型的 docker 仓库。
        • 4,创建一个 group 类型的 docker 仓库。
      • 3,小插曲。
      • 4,常规方式使用。
        • 1,配置
        • 2,重启 docker。
        • 3,pull 镜像。
        • 4,登陆私服。
        • 5,打标签。
        • 6,push 镜像。
      • 5,nginx 代理方式。
        • 1,安装 nginx。
        • 2,生成证书。
        • 3,配置 nginx。
        • 4,客户端配置。
      • 6,正式验证。
        • 1,pull 镜像。
        • 2,登陆私服。
        • 3,打标签。
        • 4,push 镜像。
        • 5,测试从私服拉镜像。
      • 7,代理的功能展示。
      • 8,参考地址。
    • 使用nexus3配置maven私有仓库
    • 使用nexus3配置npm私有仓库
    • 使用nexus3配置yum私有仓库
    • 使用nexus3配置Python私有仓库
    • 使用nexus3配置golang私有仓库
    • maven私服nexus2-14-5迁移到nexus3-7-1
    • maven上传jar包以及SNAPSHOT的一个坑
    • Gradel上传本地包的一些坑及注意点
    • go1-12与1-14之私服使用的差异与变化
    • 通过命令行往python私仓传包
    • 关于yum仓库往nexus私服传包等实践
    • 将maven本地包上传到nexus私服的实践
    • 将npm本地包上传到nexus私服的实践
    • nexus2.x升级到3.x之后仓库地址改变的问题探索
    • 关于gradle中plugins依赖未走私服导致编译失败的原因探析
  • Jenkins系列文章

  • ELK笔记

  • Kubernetes笔记

  • LLM专题

  • 系列专题
  • Nexus系列文章
二丫讲梵
2018-10-16
目录

使用nexus3配置docker私有仓库

文章发布较早,内容可能过时,阅读注意甄别。

公司已经有 harbor 作为 docker 的镜像仓库,但 Harbor 只能作为私有仓库,当需要 Docker Hub 或其他远程仓库上的镜像时,他就无能为力了。我们就只能手动到远程仓库上进行下载,再重新打标签,然后在推到 harbor 上,才能使用。

我们想要的仓库应该是既能充当私有仓库,又能在你拉取本地没有的镜像时,同步远程的公共镜像仓库,说了这些,就想起,java 项目的日常依赖解决,大概也是这么一个思路。

而为 java 项目提供私有仓库服务的 nexus3,正好也支持 docker 仓库,不仅如此,他还支持 yum, apt, npm, ruby gems, pypi 等诸多类型的仓库功能。因此,我们大概可以感受到,学会nexus3,走遍天下都不怕!

那么今天,就先来讲解研究一下,如何使用 nexus3 来配置成 docker 的私有仓库。

# 1,安装 nexus3。

这个地方略了,安装部署可以参考:nexus3 安装配置。 (opens new window)

# 2,配置走起。

# 1,创建 blob 存储。

登陆之后,先创建一个用于存储镜像的空间。

image

定义一个 name,下边的内容会自动补全。

image

然后保存。

image

这里因为我用的测试服务器演示,因此存储没有那么大,实际生产中使用,建议服务器存储 500G 或以上。

# 2,创建一个 hosted 类型的 docker 仓库。

Hosted 类型仓库用作我们的私有仓库,替代 harbor 的功能。

点击步骤如下:

image

而后可见:

image

所支持种类之丰富,可见一斑。

这里我们看到 docker 类型有三种:

  • hosted : 本地存储,即同 docker 官方仓库一样提供本地私服功能。
  • proxy : 提供代理其他仓库的类型,如 docker 中央仓库。
  • group : 组类型,实质作用是组合多个仓库为一个地址。

我们先来创建一个 hosted 类型的私有仓库。

点击 Repository下面的Repositories – Create repository – docker(hosted) :

  • Name: 定义一个名称 docker-local

  • Online: 勾选。这个开关可以设置这个 Docker repo 是在线还是离线。

  • Repository Connectors

  • 下面包含 HTTP 和 HTTPS 两种类型的 port。

  • 有什么用呢?说明讲得很清楚:

  • 连接器允许 docker 客户端直接连接到 docker 仓库,并实现一些请求操作,如 docker pull, docker push, API 查询等。但这个连接器并不是一定需要配置的,尤其是我们后面会用 group 类型的 docker 仓库来聚合它。

我们把 HTTP 这里勾选上,然后设置端口为 8083。

  • Force basic authentication

勾选。这样的话就不允许匿名访问了,执行 docker pull 或 docker push 之前,都要先登录:docker login

  • Docker Registry API Support

Docker registry 默认使用的是 API v2, 但是为了兼容性,我们可以勾选启用 API v1。

  • Storage

Blob store:我们下拉选择前面创建好的专用 blob:idocker-hub。

  • Hosted

开发环境,我们运行重复发布,因此 Delpoyment policy 我们选择 Allow redeploy。

整体配置截图如下:

image

申明

原创文章eryajf,未经授权,严禁转载,侵权必究!此乃文中随机水印,敬请读者谅解。

Copyright 二丫讲梵 (opens new window) 版权所有

# 3,创建一个 proxy 类型的 docker 仓库。

proxy 类型仓库,可以帮助我们访问不能直接到达的网络,如另一个私有仓库,或者国外的公共仓库,如官方的 dockerhub 镜像库。

创建一个 proxy 类型的仓库

  • Name: proxy-docker-hub

Repository Connectors: 不设置。

  • Proxy

    Remote Storage: docker hub 的 proxy,这里填写: https://registry-1.docker.io (opens new window) 这个是官方默认的一个链接,我这里配置使用DaoCloud (opens new window)的容器加速:http://f1361db2.m.daocloud.io (opens new window)

  • Docker Index: Use Docker Hub

  • Storage:idocker-hub

整体配置截图如下:

image

# 4,创建一个 group 类型的 docker 仓库。

group 类型的 docker 仓库,是一个聚合类型的仓库。它可以将前面我们创建的 3 个仓库聚合成一个 URL 对外提供服务,可以屏蔽后端的差异性,实现类似透明代理的功能。

  • name:docker-group

  • Repository Connectors:启用了一个监听在 8082 端口的 http 连接器;

  • Storage:选择专用的 blob 存储 idocker-hub。

  • group : 将左边可选的 3 个仓库,添加到右边的 members 下。

整体配置截图如下:

image

到这儿,nexus3 的配置算是完成了,接下来就是使用方面的事情了。

# 3,小插曲。

大家也看到了上边的截图都是比较长的,这几张图来的并不容易。在获取这些图的过程中,我尝试过自己御用的截图工具,也尝试过 Chrome 的网页截图,还尝试过其他的各种截图工具,无论是截全屏,还是滚动截图,无所不用其极,却无一能够截出令自己满意的图,哎,都是完美主义(强迫症)做的怪,于是,趁着公司已经夜深无人,我最终通过如下方式截到了心仪的长图。

2018101612290696

不为别的,只为把所分享的东西做的足够精致。当然,各位观众朋友如果有更好的截图工具,并且在 nexus3 这个界面里截出了像我截的那么长的图的话,欢迎在评论区留言分享。

# 4,常规方式使用。

请注意,这种方式经我测试,总是失败,原因还没查出来。如果有人知道原因是什么,麻烦留言区告知一下。

# 1,配置

配置/etc/docker/daemon.json, 由于不是 https,所以要在 daemon.json 中配置一下:

{ "insecure-registries": ["192.168.157.110:8082"] }
1

# 2,重启 docker。

$systemctl daemon-reload
$systemctl restart docker
1
2

# 3,pull 镜像。

docker pull redis
1

# 4,登陆私服。

docker login -u admin -p admin123 192.168.157.110:8082
1

# 5,打标签。

docker tag docker.io/redis 192.168.157.110:8082/redis
1

# 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: ""
1
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 证书的方法网上很多,这里推荐一个一键生成工具,大家可以尝试使用:

ssl

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)
1
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
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

# 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;
    }
}
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

如果有一些操作与本文不一致,那么请根据自己实际情况进行修改。

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 root@192.168.157.106:/etc/docker/certs.d/idocker.io/ca.crt
1
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
1
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
1
2

报这个错的情况下,大概原因只有一个,那就是,两台服务器的时间不一致,只需要将两台服务器时间保持一致即可。

yum -y install ntpdate && ntpdate -u cn.pool.ntp.org
1

分别在两台主机执行之后,发现登陆就成功了。

[root@docker ~]$docker login -u admin -p admin123 idocker.ioLogin Succeeded
1

# 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
1
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
1
2
3
4
5
6

这里上传成功了,再去 nexus3 里边看看是有上去了。

image

# 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
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

因为本地没有这个镜像,所以从远程仓库拉取,然后去仓库里看看啥情况:

image

这里可以看到,在我们创建的 proxy 分组中有一个 library 目录,已经自动缓存下来了这个镜像。

接着去我们用的 group 里边看看。

image

我们看到 group 里边也已经有了 gitlab 这个镜像,如果以后再有人拉取 gitlab 这个镜像,直接就可以从本地私服进行拉取了,而不用再到公网去下了,事实上,使用 nexus3 作为 maven 的私服其实也是这样一个原理,从而实现节约带宽,节约时间,等等优势。

至此,基本上关于使用 nexus3 搭建 docker 私有仓库的知识点,基本上已经知无不言,言无不尽的分享完毕了。

# 8,参考地址。

感恩前行者的付出,让我们能够走得如此顺畅,感谢自己认真钻研,从而能够把好东西用更好的方式分享出去!

  • https://zhangge.net/5139.html

  • https://segmentfault.com/a/1190000015629878#articleHeader5

微信 支付宝
#harbor#nexus#docker
上次更新: 2024/07/04, 22:40:37
nexus的安装
使用nexus3配置maven私有仓库

← nexus的安装 使用nexus3配置maven私有仓库→

最近更新
01
睡着的人不关灯
06-12
02
学习周刊-总第215期-2025年第24周
06-12
03
学习周刊-总第214期-2025年第23周
06-05
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式