Docker Private Registry

Docker Private Registry

Docker Registry

网上有很多的Registry服务器都支持第三方用户注册,而后基于用户名去做自己的仓库,但是使用互联网上的Registry有一个缺陷,那就是我们去推送和下载镜像时都不会很快,而在生产环境中很可能并行启动的容器将达到几十、上百个,而且很有可能每个服务器本地是没有镜像的,此时如果通过互联网去下载镜像会有很多问题,比如下载速度会很慢、带宽会用很多等等,如果带宽不够的话,下载至启动这个过程可能要持续个几十分钟,这已然违背了使用容器会更加轻量、快速的初衷和目的。因此,很多时候我们很有可能需要去做自己的私有Registry。

Registry用于保存docker镜像,包括镜像的层次结构和元数据。用户可以自建Registry,也可以使用官方的Docker Hub。

Docker Registry分类:

  • Sponsor Registry:第三方的Registry,供客户和Docker社区使用
  • Mirror Registry:第三方的Registry,只让客户使用
  • Vendor Registry:由发布docker镜像的供应商提供的registry
  • Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry

事实上,如果运维的系统环境托管在云计算服务上,比如阿里云,那么用阿里云的Registry则是最好的选择。很多时候我们的生产环境不会在本地,而是托管在数据中心机房里,如果我们在数据中心机房里的某台主机上部署Registry,因为都在同一机房,所以属于同一局域网,此时数据传输走内网,效率会极大的提升。

所有的Registry默认情况下都是基于https工作的,这是Docker的基本要求,而我自建Registry时很可能是基于http工作的,但是Docker默认是拒绝使用http提供Registry服务的,除非明确的告诉它,我们就是要用http协议的Registry。

Docker Private Registry

为了帮助我们快速创建私有Registry,Docker专门提供了一个名为Docker Distribution的软件包,我们可以通过安装这个软件包快速构建私有仓库。

问:既然Docker是为了运行程序的,Docker Distribution能否运行在容器中?

容器时代,任何程序都应该运行在容器中,除了Kernel和init。而为了能够做Docker Private Registry,Docker Hub官方直接把Registry做成了镜像,我们可以直接将其pull到本地并启动为容器即可快速实现私有Registry。

Registry的主要作用是托管镜像,Registry运行在容器中,而容器自己的文件系统是随着容器的生命周期终止和删除而被删除的,所以当我们把Registry运行在容器中时,客户端上传了很多镜像,随着Registry容器的终止并删除,所有镜像都将化为乌有,因此这些镜像应该放在存储卷上,而且这个存储卷最好不要放在Docker主机本地,而应该放在一个网络共享存储上,比如NFS。不过,镜像文件自己定义的存储卷,还是一个放在Docker本地、Docker管理的卷,我们可以手动的将其改成使用其它文件系统的存储卷。

这就是使用容器来运行Registry的一种简单方式。自建Registry的另一种方式,就是直接安装docker-distribution软件。

使用docker-distribution自建Registry
在node02上自建Registry

[root@localhost ~]# yum install -y  http://mirror.centos.org/centos/7/extras/x86_64/Packages/docker-distribution-2.6.2-2.git48294d9.el7.x86_64.rpm
[root@localhost ~]# systemctl start docker-distribution.service 
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                
LISTEN    0         128                      *:5000                  *:*                
LISTEN    0         128                   [::]:22                 [::]:*      

所有默认镜像仓库支持https,配置私有仓库要改协议

[root@localhost ~]# cd /etc/docker
[root@localhost docker]# ls
daemon.json  key.json
[root@localhost docker]# vim daemon.json 

{
    "registry-mirrors": ["https://4hygggbu.mirror.aliyuncs.com/"],
    "insecure-registries":["192.168.203.138:5000"]
}
EOF
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker

上传查看镜像

[root@localhost docker]# docker tag busybox:latest  192.168.203.138:5000/busybox:v1
[root@localhost docker]# docker images
192.168.203.138:5000/busybox   v1        beae173ccac6   7 months ago    1.24MB

推送镜像

[root@localhost ~]# docker push 192.168.203.138:5000/busybox:v1 
The push refers to repository [192.168.203.138:5000/busybox]
01fd6df81c8e: Pushed 
v1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527

删除镜像

[root@localhost ~]# docker rmi 192.168.203.138:5000/busybox:v1 
Untagged: 192.168.203.138:5000/busybox:v1
Untagged: 192.168.203.138:5000/busybox@sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
frices/centos      httpd     042bfc69244e   4 days ago      729MB
luojialong123/v2   latest    454d18d47b5d   6 days ago      1.24MB
luojialong123/v1   latest    b726393438ac   6 days ago      1.24MB
busybox            latest    beae173ccac6   7 months ago    1.24MB
httpd              latest    dabbfbe0c57b   7 months ago    144MB
centos             latest    5d0da3dc9764   10 months ago   231MB

在私有仓库重新拉取镜像

[root@localhost ~]# docker pull 192.168.203.138:5000/busybox:v1
v1: Pulling from busybox
Digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee
Status: Downloaded newer image for 192.168.203.138:5000/busybox:v1
192.168.203.138:5000/busybox:v1
[root@localhost ~]# docker images
REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
frices/centos                  httpd     042bfc69244e   4 days ago      729MB
luojialong123/v2               latest    454d18d47b5d   6 days ago      1.24MB
luojialong123/v1               latest    b726393438ac   6 days ago      1.24MB
busybox                        latest    beae173ccac6   7 months ago    1.24MB
192.168.203.138:5000/busybox   v1        beae173ccac6   7 months ago    1.24MB
httpd                          latest    dabbfbe0c57b   7 months ago    144MB
centos                         latest    5d0da3dc9764   10 months ago   231MB

[root@localhost ~]# curl http://192.168.203.138:5000/v2/_catalog
{"repositories":["busybox"]}

[root@localhost ~]# curl http://192.168.203.138:5000/v2/busybox/tags/list
{"name":"busybox","tags":["v1"]}

使用官方镜像自建Registry

先停止服务

[root@localhost ~]# systemctl stop docker-distribution.service 
[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                
LISTEN    0         128                   [::]:22                 [::]:*    
[root@localhost ~]# cd /etc/docker
[root@localhost docker]# ls
daemon.json  key.json
[root@localhost docker]# vim daemon.json 
{
    "registry-mirrors": ["https://4hygggbu.mirror.aliyuncs.com/"],
    "insecure-registries":["192.168.203.138:5000"]
}
EOF

[root@localhost docker]# docker run -dit --name registry -p 5000:5000 -v /opt/registry:/tmp/registry registry
[root@localhost docker]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
fc18788f4f60   registry   "/entrypoint.sh /etc…"   20 seconds ago   Up 16 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@localhost docker]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN    0         128                0.0.0.0:5000            0.0.0.0:*                
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                
LISTEN    0         128                   [::]:5000               [::]:*                
LISTEN    0         128                   [::]:22                 [::]:*             

先删除之前创建的私有仓库


[root@localhost ~]# docker images
192.168.203.138:5000/busybox   v1        beae173ccac6   7 months ago    1.24MB

[root@localhost ~]# docker rmi 192.168.203.138:5000/busybox:v1 
Untagged: 192.168.203.138:5000/busybox:v1
Untagged: 192.168.203.138:5000/busybox@sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee

上传镜像
[root@localhost ~]# docker tag busybox:latest  192.168.203.138:5000/runtime:v1
推送镜像
[root@localhost ~]# docker push 192.168.203.138:5000/runtime:v1 
The push refers to repository [192.168.203.138:5000/runtime]
01fd6df81c8e: Pushed 
v1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527
[root@localhost ~]# curl http://192.168.203.138:5000/v2/_catalog
{"repositories":["runtime"]}
[root@localhost ~]# curl http://192.168.203.138:5000/v2/runtime/tags/list
{"name":"runtime","tags":["v1"]}

Harbor

无论是使用Docker-distribution去自建仓库,还是通过官方镜像跑容器的方式去自建仓库,通过前面的演示我们可以发现其是非常的简陋的,还不如直接使用官方的Docker Hub去管理镜像来得方便,至少官方的Docker Hub能够通过web界面来管理镜像,还能在web界面执行搜索,还能基于Dockerfile利用Webhooks和Automated Builds实现自动构建镜像的功能,用户不需要在本地执行docker build,而是把所有build上下文的文件作为一个仓库推送到github上,让Docker Hub可以从github上去pull这些文件来完成自动构建。

但无论官方的Docker Hub有多强大,它毕竟是在国外,所以速度是最大的瓶颈,我们很多时候是不可能去考虑使用官方的仓库的,但是上面说的两种自建仓库方式又十分简陋,不便管理,所以后来就出现了一个被 CNCF 组织青睐的项目,其名为Harbor。

Harbor简介

Harbor是由VMWare在Docker Registry的基础之上进行了二次封装,加进去了很多额外程序,而且提供了一个非常漂亮的web界面。

Project Harbor是一个开源的可信云本地注册表项目,用于存储、签名和扫描上下文。

Harbor通过添加用户通常需要的功能,如安全性、身份和管理,扩展了开源Docker发行版。

Harbor支持用户管理、访问控制、活动监控和实例间复制等高级功能。

Harbor的功能

Feathers(羽毛):
多客户内容签名和验证
安全和漏洞分析
审核日志记录
身份集成和基于角色的访问控制
实例之间的映像复制
可扩展API和图形用户界面
国际化(目前有英文和中文)

Docker compose

Harbor在物理机上部署是非常难的,而为了简化Harbor的应用,Harbor官方直接把Harbor做成了在容器中运行的应用,而且这个容器在Harbor中依赖类似redis、mysql、pgsql等很多存储系统,所以它需要编排很多容器协同起来工作,因此VMWare Harbor在部署和使用时,需要借助于Docker的单机编排工具(Docker compose)来实现。

Compose是一个定义和运行多容器Docker应用程序的工具。使用Compose,您可以使用YAML文件来配置应用程序的服务。然后,只需一个命令,您就可以从您的配置中创建并启动所有服务。

Harbor部署

[root@localhost ~]# curl -SL https://github.com/docker/compose/releases/download/v2.7.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
[root@localhost src]# cd /usr/local/bin/

[root@localhost bin]# ls
docker-compose
做软链接
[root@localhost bin]# ln -s /usr/local/bin/docker-compose  /usr/bin/docker-compose
[root@localhost bin]# which docker-compose 
/usr/local/bin/docker-compose
查看版本
[root@localhost bin]# docker-compose version
Docker Compose version v2.7.0

 wget https://github.com/goharbor/harbor/releases/download/v2.4.3/harbor-offline-installer-v2.4.3.tgz
 [root@localhost ~]# ls
anaconda-ks.cfg  busybox  harbor-offline-installer-v2.4.3.tgz

[root@localhost ~]# ls /usr/local/
bin  etc  games  harbor  include  lib  lib64  libexec  sbin  share  src

[root@localhost harbor]# ls
common.sh  harbor.v2.4.3.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare

[root@localhost harbor]# cp harbor.yml.tmpl harbor.yml
[root@localhost harbor]# vim harbor.yml
hostname: 192.168.203.138
因为用的是私有仓库,这几行删除或者前面添加#
#https: 
# port: 443
# certificate: /your/certificate/path
  # private_key: /your/private/key/path
记住这一行是密码harbor_admin_password: Harbor12345

[root@localhost harbor]# cd /etc/docker
[root@localhost docker]# ls
daemon.json  key.json
[root@localhost docker]# vim daemon.json 

{
    "registry-mirrors": ["https://4hygggbu.mirror.aliyuncs.com/"],
    "insecure-registries":["192.168.203.138"]
}
EOF

[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker.service

[root@localhost docker]# cd /usr/local/harbor/
[root@localhost harbor]# ./install.sh 
过程省略...
✔ ----Harbor has been installed and started successfully.----

[root@localhost harbor]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN    0         128              127.0.0.1:1514            0.0.0.0:*                
LISTEN    0         128                0.0.0.0:80              0.0.0.0:*                
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                
LISTEN    0         128                   [::]:80                 [::]:*                
LISTEN    0         128                   [::]:22                 [::]:*              


[root@localhost harbor]# which docker-compose 
/usr/local/bin/docker-compose
[root@localhost harbor]# vim startall.sh
#!/bin/bash
cd /usr/local/harbor
/usr/local/bin/docker-compose stop && /usr/local/bin/docker-compose start

[root@localhost harbor]# vim /etc/rc.d/rc.local 
添加一行/bin/bash  /usr/local/harbor/startall.sh

[root@localhost harbor]# docker ps -a
CONTAINER ID   IMAGE                                COMMAND                  CREATED          STATUS                             PORTS                                   NAMES
22735e451dab   goharbor/harbor-jobservice:v2.4.3    "/harbor/entrypoint.…"   20 minutes ago   Up 4 seconds (health: starting)                                            harbor-jobservice
479964af5b2d   goharbor/nginx-photon:v2.4.3         "nginx -g 'daemon of…"   20 minutes ago   Up 4 seconds (health: starting)    0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
6abd03465fda   goharbor/harbor-core:v2.4.3          "/harbor/entrypoint.…"   20 minutes ago   Up 9 seconds (health: starting)                                            harbor-core
bd8e767e00ba   goharbor/harbor-registryctl:v2.4.3   "/home/harbor/start.…"   20 minutes ago   Up 11 seconds (health: starting)                                           registryctl
e814cca05ffb   goharbor/redis-photon:v2.4.3         "redis-server /etc/r…"   20 minutes ago   Up 11 seconds (health: starting)                                           redis
ef045ea07fda   goharbor/harbor-portal:v2.4.3        "nginx -g 'daemon of…"   20 minutes ago   Up 11 seconds (health: starting)                                           harbor-portal
eda82b7d34fa   goharbor/harbor-db:v2.4.3            "/docker-entrypoint.…"   20 minutes ago   Up 11 seconds (health: starting)                                           harbor-db
cbc197bedb68   goharbor/registry-photon:v2.4.3      "/home/harbor/entryp…"   20 minutes ago   Up 4 minutes (healthy)                                                     registry
121bb8dbb9ed   goharbor/harbor-log:v2.4.3           "/bin/sh -c /usr/loc…"   20 minutes ago   Up 4 minutes (healthy)             127.0.0.1:1514->10514/tcp               harbor-log

使用Harbor的注意事项:

1.在客户端上传镜像时一定要记得执行docker login进行用户认证,否则无法直接push
2.在客户端使用的时候如果不是用的https则必须要在客户端的/etc/docker/daemon.json配置文件中配置insecure-registries参数
3.数据存放路径应在配置文件中配置到一个容量比较充足的共享存储中
4.Harbor是使用docker-compose命令来管理的,如果需要停止Harbor也应用docker-compose stop来停止,其他参数请–help

posted @   罗家龙  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示