docker 镜像仓库介绍

 

 

Docker 仓库管理
Docker仓库,类似于yum仓库,是用来保存镜像的仓库。


为了方便的管理和使用docker镜像,可以将镜像集中保存至Docker仓库中,将制作好的镜像push到仓库
集中保存,在需要镜像时,从仓库中pull镜像即可。
Docker 仓库分为公有云仓库和私有云仓库
公有云仓库: 由互联网公司对外公开的仓库

  • 官方 hub.docker.com
  • 阿里云等第三方仓库

私有云仓库: 组织内部搭建的仓库,一般只为组织内部使用,常使用下面软件搭建仓库

  • docker registory: docker 官方
  • docker harbor:vmware 官方

官方 Docker 仓库
将自制的镜像上传至docker仓库;https://hub.docker.com

 

Docker 之分布式仓库 Harbor (重点)

Harbor 介绍和架构

Harbor 介绍

image

 

Harbor (港口,海港) 是一个用于存储和分发Docker镜像的企业级Registry服务器,由VMware开源,其通
过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源 Docker Distribution。
作为一个企业级私有Registry服务器,Harbor 提供了更好的性能和安全。提升用户使用Registry构建和
运行环境传输镜像的效率。Harbor支持安装在多个Registry节点的镜像资源复制,镜像全部保存在私有
Registry 中, 确保数据和知识产权在公司内部网络中管控,另外,Harbor也提供了高级的安全特性,诸
如用户管理,访问控制和活动审计等
vmware 官方开源服务: https://vmware.github.io/
harbor 官方github 地址: https://github.com/vmware/harbor
harbor 官方网址: https://goharbor.io/
harbor 官方文档: https://goharbor.io/docs/
github文档: https://github.com/goharbor/harbor/tree/master/docs

 

Harbor 功能官方介绍

基于角色的访问控制: 用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限

镜像复制: 镜像可在多个Registry实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景

图形化用户界面: 用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间
AD/LDAP 支: Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理
审计管理: 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理
国际化: 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来
RESTful API: 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易
部署简单: 提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备

 

 Harbor 组成

image

 harbor是由很多容器组成实现完整功能

Proxy: 对应启动组件nginx。它是一个nginx反向代理,代理Notary client(镜像认证)、Docker client(镜像上传下载等)和浏览器的访问请求(Core Service)给后端的各服务

UI(Core Service): 对应启动组件harbor-ui。底层数据存储使用mysql数据库,主要提供了四个子功能:
UI: 一个web管理页面ui
API: Harbor暴露的API服务
Auth: 用户认证服务,decode后的token中的用户信息在这里进行认证;auth后端可以接
db、ldap、uaa三种认证实现
Token服务(上图中未体现): 负责根据用户在每个project中的role来为每一个docker push/pull命令发布一个token,如果从docker client发送给registry的请求没有带token,registry会重定向请求到token服务创建token


Registry: 对应启动组件registry。负责存储镜像文件,和处理镜像的pull/push命令。Harbor对镜像进行强制的访问控制,Registry会将客户端的每个pull、push请求转发到token服务来获取有效的token
Admin Service: 对应启动组件harbor-adminserver。是系统的配置管理中心附带检查存储用量,
ui和jobserver启动时候需要加载adminserver的配置
Job Sevice: 对应启动组件harbor-jobservice。负责镜像复制工作的,他和registry通信,从一个
registry pull镜像然后push到另一个registry,并记录job_log
Log Collector: 对应启动组件harbor-log。日志汇总组件,通过docker的log-driver把日志汇总到一起
DB: 对应启动组件harbor-db,负责存储project、 user、 role、replication、image_scan、access等的metadata数据

安装 Harbor
下载地址: https://github.com/vmware/harbor/releases
安装文档:

https://goharbor.io/docs/2.12.0/install-config/
https://github.com/goharbor/harbor/blob/master/docs/install-config/_index.md
https://goharbor.io/docs/2.10.0/install-config/

 

 

安装 docker

先安装docker compose

下载Harbor安装包并解压缩

https://github.com/goharbor/harbor/releases

方法1: 下载离线完整安装包,推荐使用

VERSION=v2.15.0

wget https://github.com/goharbor/harbor/releases/download/${VERSION}/harbor-offline-installer-${VERSION}.tgz

解压缩离线包

tar -xf harbor-offline-installer-v2.15.0.tgz  -C /usr/local/

root@master2 ~]# ls /usr/local/harbor
common.sh  harbor.v2.15.0.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare

编辑 harbor 配置文件
最新文档: https://github.com/goharbor/harbor/blob/master/docs/install-config/configure-yml-file.md
 cd /usr/local/harbor/

docker load -i harbor.v2.15.0.tar.gz 

#新版配置文件为yml格式

cp -a /usr/local/harbor/harbor.yml.tmpl /usr/local/harbor/harbor.yml

#只需要修改下面两行
hostname = 192.168.3.61         #修改此行,指向当前主机IP 或 FQDN,建议配置IP
harbor_admin_password = 123456 #修改此行指定harbor登录用户admin的密码,默认用户/密码:admin/Harbor12345

data_volume: /data/harbor      #建议修改数据目录路径,使用大容量的高速磁盘,默认为/data

 

#如果不使用https,还需要将下面行注释掉
#https:
# port: 443
# certificate: /your/certificate/path
# private_key: /your/private/key/path

 

#可选项
ui_url_protocol = http #默认即可,如果修改为https,需要指定下面证书路径
ssl_cert = /data/cert/server.crt #默认即可,https时,需指定下面证书文件路径
ss_cert_key = /data/cert/server.key   #默认即可,https时,需指定下面私钥文件路径

 

运行 harbor 安装脚本

安装脚本选项

-with-trivy:
Trivy 是一个由 Aqua Security 开发的开源漏洞扫描器,用于检测容器镜像(如 Docker 镜像)中的已
知漏洞。
使用 --with-trivy 选项,你将在 Harbor 中启用 Trivy 的集成。这意味着你可以在 Harbor 中直接
扫描你的镜像以查找潜在的漏洞。
#新版下面选项废弃 --with-chartmuseum: Chartmuseum 是一个用于存储和分发 Helm 包的轻量级 Helm Chart 仓库。Helm 是一个 Kubernetes 的包管理器,用于定义、安装和升级 Kubernetes 应用程序。 使用 --with-chartmuseum 选项,你将在 Harbor 中启用 Chartmuseum 的集成。这样,你就可以在 Harbor 中存储、管理和分发 Helm Charts 了。
#注意,harbor从2.8.0开始已经不支持chartmuseum --with-notary: Notary 是一个用于对 Docker 镜像进行签名和验证的工具,以确保镜像的完整性和来源。 使用 --with-notary 选项,你将在 Harbor 中启用 Notary 的集成。这样,你可以使用 Notary 来对 你的镜像进行签名,并使用 Harbor 的验证功能来确保只有经过签名的镜像才能被推送到仓库或从仓库中拉 取。

 

范例:

#安装docker harbor并开启Trivy 扫描器

/usr/local/harbor/install.sh --with-trivy

 

[root@master2 harbor]# docker ps -a
CONTAINER ID   IMAGE                                   COMMAND                  CREATED              STATUS                        PORTS                                     NAMES
2f7d8d114083   goharbor/harbor-jobservice:v2.15.0      "/harbor/entrypoint.…"   About a minute ago   Up About a minute (healthy)                                             harbor-jobservice
afe843c4f39f   goharbor/nginx-photon:v2.15.0           "nginx -g 'daemon of…"   About a minute ago   Up About a minute (healthy)   0.0.0.0:80->8080/tcp, [::]:80->8080/tcp   nginx
0848a2613b6b   goharbor/harbor-core:v2.15.0            "/harbor/entrypoint.…"   About a minute ago   Up About a minute (healthy)                                             harbor-core
7f9e53df306e   goharbor/trivy-adapter-photon:v2.15.0   "/home/scanner/entry…"   About a minute ago   Up About a minute (healthy)                                             trivy-adapter
5a5e8a4dc7c2   goharbor/harbor-registryctl:v2.15.0     "/home/harbor/start.…"   About a minute ago   Up About a minute (healthy)                                             registryctl
4c8d5149e15e   goharbor/harbor-portal:v2.15.0          "nginx -g 'daemon of…"   About a minute ago   Up About a minute (healthy)                                             harbor-portal
3739a9563b0d   goharbor/harbor-db:v2.15.0              "/docker-entrypoint.…"   About a minute ago   Up About a minute (healthy)                                             harbor-db
3a1736dcb720   goharbor/redis-photon:v2.15.0           "redis-server /etc/r…"   About a minute ago   Up About a minute (healthy)                                             redis
30c3afbd497c   goharbor/registry-photon:v2.15.0        "/home/harbor/entryp…"   About a minute ago   Up About a minute (healthy)                                             registry
87b018cac8fd   goharbor/harbor-log:v2.15.0             "/bin/sh -c /usr/loc…"   About a minute ago   Up About a minute (healthy)   127.0.0.1:1514->10514/tcp                 harbor-log
[root@master2 harbor]# 

 

 

修改 docker-compose.yaml文件解决地址冲突(可选)

如果自定义网络地址冲突,可以修改如下文件

vim /usr/local/harbor/docker-compose.yml

networks:
 harbor:          #添加如下面内容
   ipam:
     driver: default
     config:
      - subnet: 172.27.0.0/16

cd /usr/local/harbor/

docker-compose down;docker-compose up -d

 

 

#安装harbor后会自动开启很多相关容器

登录 harbor 主机网站

用浏览器访问: http://192.168.3.61/
用户名: admin
密码: 即前面harbor.yml中指定的密码

 

使用单主机 Harbor

建立项目

harbor上必须先建立项目,才能上传镜像

image

 

创建用户和项目授权

用户角色

  • 项目管理员:可以创建和删除项目
  • 维护人员:可以上传,删除镜像,但无法删除项目
  • 开发者:可以上传镜像,但无法删除镜像
  • 访客,受限访客:无法上传镜像

创建用户

 在项目内对用户授权,注意:至少是开发者以上才能上传镜像

image

 

命令行登录 Harbor

 

 

192.168.3.61 harbor.ming.org

root@master2 harbor]# tail -n 1 /etc/hosts
192.168.3.61 harbor.ming.org

 

#方法2

root@master2 harbor]# cat /etc/docker/daemon.json    
{
   "registry-mirrors": [
       "https://docker.m.daocloud.io",
       "https://docker.1panel.live",
       "https://docker.1ms.run",
       "https://docker.xuanyuan.me"
  ],
   "insecure-registries": ["harbor.ming.org","192.168.3.61"]
}
[root@master2 harbor]# 

systemctl restart docker

#非交互登录

 docker login -u jack -p Aa123456 192.168.3.61

docker login -u jack -p Aa123456 harbor.ming.org

 

Login Succeeded
[root@master2 harbor]# cat /root/.docker/config.json
{
        "auths": {
                "192.168.3.61": {
                        "auth": "amFjazpBYTEyMzQ1Ng=="
                }
        }
}[root@master2 harbor]# 

 

[root@master2 harbor]# cat /root/.docker/config.json
{
        "auths": {
                "192.168.3.61": {
                        "auth": "amFjazpBYTEyMzQ1Ng=="
                },
                "harbor.ming.org": {
                        "auth": "amFjazpBYTEyMzQ1Ng=="
                }
        }
}[root@master2 harbor]# 
[root@master2 harbor]# 

 

给本地镜像打标签并上传到 Harbor

修改 images 的名称,不修改成指定格式无法将镜像上传到 harbor 仓库
格式为:

  • Harbor主机IP/项目名/image名:版本

上传的镜像默认存放在Harbor的 /data/registry/ 目录中

docker pull nginx:1.30

docker tag nginx:1.30  192.168.3.61/example/nginx:1.30

docker push 192.168.3.61/example/nginx:1.30

 

 docker tag nginx:1.30  harbor.ming.org/example/nginx:1.30

docker push harbor.ming.org/example/nginx:1.30 

 

访问harbor网站验证上传镜像成功


下载 Harbor 的镜像

root@master2 harbor]# tail -n 1 /etc/hosts
192.168.3.61 harbor.ming.org

 

docker pull 192.168.3.61/example/nginx:1.30

docker pull harbor.ming.org/example/nginx:1.30

 

修改 Harbor 配置
后期如果修改harbor配置,比如: 修改IP地址等,可执行以下步骤生效

方法1: 

cd /usr/local/harbor/

docker-compose stop

#所有相关容器都退出

docker ps -a

#修改harbor配置

vim harbor.cfg

#更新配置

/usr/local/harbor/prepare 

#重新启动docker compose

docker-compose start

 

方法2: 

/usr/local/harbor/install.sh

 

Harbor 安全 Https 配置

基于安全考虑,生产建议采用 https 代替 http
新版2.5.0的Https实现方法出现了一些变化
官方文档

https://goharbor.io/docs/2.14.0/install-config/configure-https/

生成 Harbor 服务器证书

 

#创建证书相关数据的目录
mkdir -p /data/harbor/certs
cd /data/harbor/certs

#生成ca的私钥
openssl genrsa -out ca.key 4096

 

#生成ca的自签名证书
openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=ca.ming.org" \
-key ca.key \
-out ca.crt

 

#生成harbor主机的私钥
openssl genrsa -out harbor.ming.org.key 4096

 

#生成harbor主机的证书申请,注意:CN的名称一定是访问harbor的主机域名
openssl req -sha512 -new \
   -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.ming.org" \
   -key harbor.ming.org.key \
   -out  harbor.ming.org.csr

 

#创建x509 v3 扩展文件(新版新增加的要求) 

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names


[alt_names]
DNS.1=harbor.ming.org      
DNS.2=ming                 #可选
DNS.3=ming.org             #可选
IP.1=192.168.3.61          #可选
EOF

 

#此处必须和和harbor的网站名称一致

 

#给harbor主机颁发证书

openssl x509 -req -sha512 -days 3650 \
   -extfile v3.ext \
   -CA ca.crt -CAkey ca.key -CAcreateserial \
   -in harbor.ming.org.csr \
   -out harbor.ming.org.crt

 

openssl x509 -in harbor.ming.org.crt -noout -text

 

#新版的最终文件列表如下

root@master2 certs]# ls
ca.crt  ca.key  ca.srl  harbor.ming.org.crt  harbor.ming.org.csr  harbor.ming.org.key  v3.ext
[root@master2 certs]# 

配置 Harbor 服务器使用证书

vim /usr/local/harbor/harbor.yml

......
hostname: harbor.ming.org #注意:此行必须是网站的域名,而且harbor主机的/etc/hosts可以不
解析此域名,不能是IP地址,否则登录时会报如下错误
Error response from daemon: Get "https://harbor.ming.org/v2/": Get 
"https://10.0.0.203/service/token?
account=admin&client_id=docker&offline_token=true&service=harbor-registry": 
x509: cannot validate certificate for 10.0.0.203 because it doesn't contain any 
IP SANs
# https related config
https:
  # https port for harbor, default is 443
 port: 443
  # The path of cert and key files for nginx
 certificate: /data/harbor/certs/harbor.ming.org.crt
 private_key: /data/harbor/certs/harbor.ming.org.key
......

 

 

# Configuration file of Harbor

# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: harbor.ming.org

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 80

# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /data/harbor/certs/harbor.ming.org.crt
  private_key: /data/harbor/certs/harbor.ming.org.key
  # enable strong ssl ciphers (default: false)
  # strong_ssl_ciphers: false

# # Harbor will set ipv4 enabled only by default if this block is not configured
# # Otherwise, please uncomment this block to configure your

 

#使上面的配置生效

cd /usr/local/harbor/

./prepare

docker-compose down -v
docker-compose up -d

 

 

[root@master2 harbor]# docker ps -a
CONTAINER ID   IMAGE                                   COMMAND                  CREATED              STATUS                        PORTS                                                                                NAMES
ceeb4e4d42f3   goharbor/nginx-photon:v2.15.0           "nginx -g 'daemon of…"   About a minute ago   Up About a minute (healthy)   0.0.0.0:80->8080/tcp, [::]:80->8080/tcp, 0.0.0.0:443->8443/tcp, [::]:443->8443/tcp   nginx
e0dd142efeb8   goharbor/harbor-jobservice:v2.15.0      "/harbor/entrypoint.…"   About a minute ago   Up About a minute (healthy)                                                                                        harbor-jobservice
b220c217bbe1   goharbor/harbor-core:v2.15.0            "/harbor/entrypoint.…"   About a minute ago   Up About a minute (healthy)                                                                                        harbor-core
3123832f6feb   goharbor/harbor-db:v2.15.0              "/docker-entrypoint.…"   About a minute ago   Up About a minute (healthy)                                                                                        harbor-db
5d779de4ad7a   goharbor/registry-photon:v2.15.0        "/home/harbor/entryp…"   About a minute ago   Up About a minute (healthy)                                                                                        registry
eb63b62c22b4   goharbor/harbor-registryctl:v2.15.0     "/home/harbor/start.…"   About a minute ago   Up About a minute (healthy)                                                                                        registryctl
efec51c01932   goharbor/harbor-portal:v2.15.0          "nginx -g 'daemon of…"   About a minute ago   Up About a minute (healthy)                                                                                        harbor-portal
08c1e697f868   goharbor/redis-photon:v2.15.0           "redis-server /etc/r…"   About a minute ago   Up About a minute (healthy)                                                                                        redis
a5f805a95a9c   goharbor/harbor-log:v2.15.0             "/bin/sh -c /usr/loc…"   About a minute ago   Up About a minute (healthy)   127.0.0.1:1514->10514/tcp                                                            harbor-log
7f9e53df306e   goharbor/trivy-adapter-photon:v2.15.0   "/home/scanner/entry…"   5 hours ago          Up 2 hours (healthy)                                                                                               trivy-adapter
[root@master2 harbor]# netstat -tnpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      28888/docker-proxy  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      633/sshd: /usr/sbin 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28867/docker-proxy  
tcp        0      0 127.0.0.1:1514          0.0.0.0:*               LISTEN      28317/docker-proxy  
tcp6       0      0 :::443                  :::*                    LISTEN      28895/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      633/sshd: /usr/sbin 
tcp6       0      0 :::80                   :::*                    LISTEN      28871/docker-proxy  

 

输入下面 http 链接自动跳转到 https

http://harbor.ming.org

 

配置 Docker 客户端使用证书文件

docker logout harbor.ming.org

docker logout 192.168.3.61

[root@master2 harbor]# cat /root/.docker/config.json 
{
        "auths": {}
}[root@master2 harbor]# 

 

方法1:官方方法

 

cp -a harbor.ming.org.crt harbor.ming.org.cert

#比较两个文件的不同

 md5sum harbor.ming.org.crt harbor.ming.org.cert

 

#创建和harbor服务器同名的目录

mkdir -p /etc/docker/certs.d/harbor.ming.org/ 

 

#在docker客户端使用上面的证书文件
#注意:官方介绍需要同时复制harbor.wang.org.key ca.crt,v2.14.2

cp -a harbor.ming.org.cert harbor.ming.org.key ca.crt /etc/docker/certs.d/harbor.ming.org/

#在客户端上要求有下面三个文件

[root@master2 certs]# ls /etc/docker/certs.d/harbor.ming.org/
ca.crt  harbor.ming.org.cert  harbor.ming.org.key

 

Docker 客户端登录Harbor

#在docker客户端登录harbor服务器,注意:此时无需再配置insecure-registries项即可登录

 

[root@master2 certs]# 
[root@master2 certs]# vim /etc/docker/daemon.json 
{
   "registry-mirrors": [
       "https://docker.m.daocloud.io",
       "https://docker.1panel.live",
       "https://docker.1ms.run",
       "https://docker.xuanyuan.me"
  ]
}  

 

#新版如果无法登录,需重启docker服务
systemctl restart docker

 

docker login -u admin -p 123456 harbor.ming.org

登录成功

[root@master2 harbor]# docker login -u admin -p 123456 harbor.ming.org
WARNING! Using --password via the CLI is insecure. Use --password-stdin.

WARNING! Your credentials are stored unencrypted in '/root/.docker/config.json'.
Configure a credential helper to remove this warning. See
https://docs.docker.com/go/credential-store/

Login Succeeded

#登录成功后,自动保存登录信息,注意:auth对应的密码使用base64编码,可以还原明文密码

[root@master2 harbor]# cat /root/.docker/config.json 
{
        "auths": {
                "harbor.ming.org": {
                        "auth": "YWRtaW46MTIzNDU2"
                }
        }
}[root@master2 harbor]# 

 

 

不信任

[root@master2 harbor]# docker pull harbor.ming.org/example/nginx:1.30
Error response from daemon: failed to resolve reference "harbor.ming.org/example/nginx:1.30": failed to authorize: failed to fetch oauth token: Post "https://harbor.ming.org/service/token": tls: failed to verify certificate: x509: certificate signed by unknown authority

 

让所有docker客户端信任私有CA的证书

#将私有CA的证书加入到每个docker主机的上信任证书CA列表中,在所有Harbor客户端执行

 

cat /data/harbor/certs/ca.crt >> /etc/ssl/certs/ca-certificates.crt

#将上面的文件复制到所有docker主机覆盖原文件

#并将所有docker主机的docker服务重启生效
systemctl restart docker.service

 

Docker 客户端测试推送和拉取镜像

 

 

验证拉取

docker pull harbor.ming.org/example/nginx:1.30

[root@master2 harbor]# docker pull harbor.ming.org/example/nginx:1.30
1.30: Pulling from example/nginx
Digest: sha256:82e0252fa9af07c1d9c9ff56beb29976e8739ad78c7c87cc629bed9ceb2502e7
Status: Image is up to date for harbor.ming.org/example/nginx:1.30
harbor.ming.org/example/nginx:1.30

 

验证推送

docker pull alpine:3.23.3

docker tag alpine:3.23.3 harbor.ming.org/example/alpine:3.23.3

 docker push harbor.ming.org/example/alpine:3.23.3

 

posted @ 2026-07-04 16:25  minger_lcm  阅读(23)  评论(0)    收藏  举报