Docker分别使用Registry和Harbor搭建私有仓库
Tags: DockerHarborLinuxRegistry企业私有仓库
一、简介
在 Docker 中,当我们执行 docker pull xxx 的时候 ,它实际上是从 registry.hub.docker.com 这个地址去查找,这就是Docker公司为我们提供的公共仓库。在工作中,我们不可能把企业项目push到公有仓库进行管理。所以为了更好的管理镜像,Docker不仅提供了一个中央仓库,同时也允许我们搭建本地私有仓库。
Harbor和Registry的比较
Harbor和Registry都是Docker的镜像仓库,但是Harbor作为更多企业的选择,是因为相比较于Regisrty来说,它具有很多的优势。
1.提供分层传输机制,优化网络传输
Docker镜像是是分层的,而如果每次传输都使用全量文件(所以用FTP的方式并不适合),显然不经济。必须提供识别分层传输的机制,以层的UUID为标识,确定传输的对象。
2.提供WEB界面,优化用户体验
只用镜像的名字来进行上传下载显然很不方便,需要有一个用户界面可以支持登陆、搜索功能,包括区分公有、私有镜像。
3.支持水平扩展集群
当有用户对镜像的上传下载操作集中在某服务器,需要对相应的访问压力作分解。
4.良好的安全机制
企业中的开发团队有很多不同的职位,对于不同的职位人员,分配不同的权限,具有更好的安全性。
5.Harbor提供了基于角色的访问控制机制,并通过项目来对镜像进行组织和访问权限的控制。kubernetes中通过namespace来对资源进行隔离,在企业级应用场景中,通过将两者进行结合可以有效将kubernetes使用的镜像资源进行管理和访问控制,增强镜像使用的安全性。尤其是在多租户场景下,可以通过租户、namespace和项目相结合的方式来实现对多租户镜像资源的管理和访问控制。
二、registry 的搭建
搭建
Docker 官方提供了一个搭建私有仓库的镜像 registry ,只需把镜像下载下来,运行容器并暴露5000端口,就可以使用了。
官网:https://hub.docker.com/_/registry?tab=tags&page=1&ordering=last_updated
1 2 | docker pull registry:latest docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --name myregistry registry:latest |
Registry服务默认会将上传的镜像保存在容器的/var/lib/registry,我们将主机的/opt/registry目录挂载到该目录,即可实现将镜像保存到主机的/opt/registry目录了。
浏览器访问http://127.0.0.1:5000/v2,出现下面情况说明registry运行正常。
验证
现在通过push镜像到registry来验证一下。
查看本地镜像:
1 2 3 4 5 | $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 568c4670fa80 5 weeks ago 109MB ubuntu latest 93fd78260bd1 7 weeks ago 86.2MB elasticsearch 6.5.1 32f93c89076d 7 weeks ago 773MB |
要通过docker tag将该镜像标志为要推送到私有仓库:
1 | docker tag nginx:latest localhost:5000/nginx:latest |
通过 docker push 命令将 nginx 镜像 push到私有仓库中:
1 | docker push localhost:5000/nginx:latest |
访问 http://127.0.0.1:5000/v2/_catalog 查看私有仓库目录,可以看到刚上传的镜像了:
下载私有仓库的镜像,使用如下命令:
1 2 3 | docker pull localhost:5000/镜像名:版本号 -- 例如 docker pull localhost:5000/nginx:latest |
报错解决
若是在其它docker的客户端上传报错:
1 2 3 | [root@docker36 harbor]# docker push 192.168.1.35:5000/nginx:alpine2 The push refers to repository [192.168.1.35:5000/nginx] Get https://192.168.1.35:5000/v2/: http: server gave HTTP response to HTTPS client |
出现这问题的原因是:Docker自从1.3.X之后docker registry交互默认使用的是HTTPS,但是搭建私有镜像默认使用的是HTTP服务,所以与私有镜像交时出现以上错误。
解决,vim /etc/docker/daemon.json
,增加如下内容:
1 | { "insecure-registries":["192.168.1.100:5000"] } |
例如,我的文件增加后:
1 2 3 4 5 6 7 8 9 10 11 | [root@docker36 certs.d]# more /etc/docker/daemon.json { "registry-mirrors": [ "https://hub.daocloud.io", "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn", "https://vm1wbfhf.mirror.aliyuncs.com", "https://pee6w651.mirror.aliyuncs.com" ], "insecure-registries": ["192.168.1.35:5000"] } |
然后,重启docker。
1 2 3 4 5 6 7 8 9 10 | [root@docker36 harbor]# systemctl restart docker [root@docker36 harbor]# [root@docker36 harbor]# docker push 192.168.1.35:5000/nginx:alpine2 The push refers to repository [192.168.1.35:5000/nginx] 22b34b04730a: Pushed 0abec5b5c783: Pushed 25b4fafa93fe: Pushed d9ff549177a9: Pushed alpine2: digest: sha256:d25ed0a8c1b4957f918555c0dbda9d71695d7b336d24f7017a87b2081baf1112 size: 1153 [root@docker36 harbor]# |
三、harbor 的搭建
简介
docker 官方提供的私有仓库 registry,用起来虽然简单 ,但在管理的功能上存在不足。 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,harbor使用的是官方的docker registry(v2命名是distribution)服务去完成。harbor在docker distribution的基础上增加了一些安全、访问控制、管理的功能以满足企业对于镜像仓库的需求。
- 虽然Docker官方提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。
- Harbor是由VMware公司开源的企业级的Docker Registry管理项目,相比docker官方拥有更丰富的权限权利和完善的架构设计,适用大规模docker集群部署提供仓库服务。
- 它主要提供 Dcoker Registry 管理界面UI,可基于角色访问控制,镜像复制, AD/LDAP 集成,日志审核等功能,完全的支持中文。
Harbor核心组件解释
Harbor在架构上主要由6个组件构成:
Proxy:Harbor的registry, UI, token等服务,通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务。
Registry: 负责储存Docker镜像,并处理docker push/pull 命令。由于我们要对用户进行访问控制,即不同用户对Docker image有不同的读写权限,Registry会指向一个token服务,强制用户的每次docker pull/push请求都要携带一个合法的token, Registry会通过公钥对token 进行解密验证。
本人提供Oracle、MySQL、PG等数据库的培训和考证业务,私聊QQ646634621或微信db_bao,谢谢!Core services: 这是Harbor的核心功能,主要提供以下服务:
- UI:提供图形化界面,帮助用户管理registry上的镜像(image), 并对用户进行授权。
- webhook:为了及时获取registry 上image状态变化的情况, 在Registry上配置webhook,把状态变化传递给UI模块。
- token 服务:负责根据用户权限给每个docker push/pull命令签发token. Docker 客户端向Regiøstry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向Registry进行请求。
Database:为core services提供数据库服务,负责储存用户权限、审计日志、Docker image分组信息等数据。
Job Services:提供镜像远程复制功能,可以把本地镜像同步到其他Harbor实例中。
Log collector:为了帮助监控Harbor运行,负责收集其他组件的log,供日后进行分析。
Harbor特性
- 基于角色的访问控制 :用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。
- 镜像复制 : 镜像可以在多个Registry实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。
- 图形化用户界面 : 用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。
- AD/LDAP 支持 : Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。
- 审计管理 : 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。
- 国际化 : 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。
- RESTful API : RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。
- 部署简单 : 提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。
harbor搭建
下载地址:https://github.com/goharbor/harbor/releases
官网:https://goharbor.io/docs/2.3.0/
最新版本:2.3.1
安装包大小:600MB
安装条件: docker 17.06.0-ce+ 且 docker-compose 1.18.0+
Download binaries of Harbor release and follow Installation & Configuration Guide to install Harbor.
下载并解压:
1 2 3 4 5 6 7 8 9 10 11 12 | wget https://github.com/goharbor/harbor/releases/download/v2.3.1/harbor-offline-installer-v2.3.1.tgz tar -xvf harbor-offline-installer-v2.3.1.tgz cd harbor [root@docker36 harbor]# ll -h total 604M -rw-r--r-- 1 root root 12K Jul 19 18:45 LICENSE -rw-r--r-- 1 root root 3.3K Jul 19 18:45 common.sh -rw-r--r-- 1 root root 604M Jul 19 18:46 harbor.v2.3.1.tar.gz -rw-r--r-- 1 root root 7.7K Jul 19 18:45 harbor.yml.tmpl -rwxr-xr-x 1 root root 2.5K Jul 19 18:45 install.sh -rwxr-xr-x 1 root root 1.9K Jul 19 18:45 prepare |
harbor启用https访问
https://goharbor.io/docs/2.3.0/install-config/configure-https/
https://www.cnblogs.com/cjwnb/p/13441071.html
默认情况下,Harbor不附带证书。可以在没有安全性的情况下部署Harbor,以便您可以通过HTTP连接到它。但是,只有在没有外部网络连接的空白测试或开发环境中,才可以使用HTTP。在没有空隙的环境中使用HTTP会使您遭受中间人攻击。在生产环境中,请始终使用HTTPS。如果启用Content Trust with Notary来正确签名所有图像,则必须使用HTTPS。
要配置HTTPS,必须创建SSL证书。您可以使用由受信任的第三方CA签名的证书,也可以使用自签名证书。
注:高版本(14以上)docker执行login命令,默认使用https,且harbor必须使用域名,不建议IP地址,且后期使用IP方式可能废弃。
假设使用的网址是:www.xmmharbor.com,本机ip是192.168.1.36:
因为这个网址是虚拟的,所以需要在本机hosts文件中添加
1 | echo "192.168.1.36 www.xmmharbor.com" >> /etc/hosts |
修改harobr.yml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | cp harbor.yml.tmpl harbor.yml vi harbor.yml #hostname 改为一个域名 hostname: www.xmmharbor.com #密码 harbor_admin_password: Harbor12345 #数据目录 data_volume: /var/lib/docker/data https: # https port for harbor, default is 443 port: 443 # The path of cert and key files for nginx certificate: /data/cert/www.xmmharbor.com.crt private_key: /data/cert/www.xmmharbor.com.key # 注意证书路径 |
一键生成密钥脚本文件:
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 44 45 46 | #!/bin/bash # 在该目录下操作生成证书,正好供harbor.yml使用 mkdir -p /data/cert cd /data/cert openssl genrsa -out ca.key 4096 openssl req -x509 -new \ -nodes -sha512 -days 3650 \ -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=www.xmmharbor.com" \ -key ca.key \ -out ca.crt openssl genrsa -out www.xmmharbor.com.key 4096 openssl req -sha512 -new \ -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=www.xmmharbor.com" \ -key www.xmmharbor.com.key \ -out www.xmmharbor.com.csr cat > v3.ext <<-EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment extendedKeyUsage = serverAuth subjectAltName = @alt_names [alt_names] DNS.1=www.xmmharbor.com DNS.2=www.xmmharbor.com DNS.3=www.xmmharbor.com EOF openssl x509 -req -sha512 -days 3650 \ -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial \ -in www.xmmharbor.com.csr \ -out www.xmmharbor.com.crt openssl x509 -inform PEM -in www.xmmharbor.com.crt -out www.xmmharbor.com.cert mkdir -p /etc/docker/certs.d/www.xmmharbor.com/ /bin/cp www.xmmharbor.com.cert /etc/docker/certs.d/www.xmmharbor.com/ /bin/cp www.xmmharbor.com.key /etc/docker/certs.d/www.xmmharbor.com/ /bin/cp ca.crt /etc/docker/certs.d/www.xmmharbor.com/ update-ca-trust |
最终docker目录结构:
1 2 3 4 5 6 7 8 9 | [root@docker36 ~]# tree /etc/docker/certs.d/www.xmmharbor.com/ /etc/docker/certs.d/www.xmmharbor.com/ |-- ca.crt |-- www.xmmharbor.com.cert `-- www.xmmharbor.com.key 0 directories, 3 files |
开始安装
通过运行 install.sh 构建镜像,并把服务启动起来:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | [root@docker36 harbor]# ./install.sh [Step 0]: checking if docker is installed ... Note: docker version: 19.03.12 [Step 1]: checking docker-compose is installed ... Note: docker-compose version: 1.26.2 [Step 2]: loading Harbor images ... 41648b8ffd20: Loading layer [==================================================>] 37.21MB/37.21MB 17517bb678cd: Loading layer [==================================================>] 9.914MB/9.914MB 7be8271fd6c7: Loading layer [==================================================>] 17.67MB/17.67MB 9a44ae952baa: Loading layer [==================================================>] 4.608kB/4.608kB c179ae9f4bb4: Loading layer [==================================================>] 18.46MB/18.46MB Loaded image: goharbor/harbor-exporter:v2.3.1 b3fe5603c553: Loading layer [==================================================>] 6.181MB/6.181MB b0b3b7027bf9: Loading layer [==================================================>] 6.207MB/6.207MB 5aabddd05f7d: Loading layer [==================================================>] 14.47MB/14.47MB 56dacaf676bf: Loading layer [==================================================>] 29.29MB/29.29MB c9199a5d2e42: Loading layer [==================================================>] 22.02kB/22.02kB 227ae5e03e36: Loading layer [==================================================>] 14.47MB/14.47MB Loaded image: goharbor/notary-signer-photon:v2.3.1 ae3b7f58d662: Loading layer [==================================================>] 41.95MB/41.95MB f670a46c0b96: Loading layer [==================================================>] 4.096kB/4.096kB ab617bd5330b: Loading layer [==================================================>] 3.072kB/3.072kB 9476ba967537: Loading layer [==================================================>] 31.52MB/31.52MB 993405872011: Loading layer [==================================================>] 11.39MB/11.39MB 675afd4bd758: Loading layer [==================================================>] 43.7MB/43.7MB Loaded image: goharbor/trivy-adapter-photon:v2.3.1 891c37c24656: Loading layer [==================================================>] 8.112MB/8.112MB fb11e66e79e5: Loading layer [==================================================>] 11.64MB/11.64MB 48fc1e32997f: Loading layer [==================================================>] 1.688MB/1.688MB Loaded image: goharbor/harbor-portal:v2.3.1 ed74a6a7b440: Loading layer [==================================================>] 161MB/161MB d01f3ed208d8: Loading layer [==================================================>] 3.584kB/3.584kB 56498e347596: Loading layer [==================================================>] 3.072kB/3.072kB b2f51c8b45a8: Loading layer [==================================================>] 2.56kB/2.56kB c32a505aa2f0: Loading layer [==================================================>] 3.072kB/3.072kB 2c7f77f6876c: Loading layer [==================================================>] 3.584kB/3.584kB 2e197003cccc: Loading layer [==================================================>] 19.97kB/19.97kB Loaded image: goharbor/harbor-log:v2.3.1 7a40b6380552: Loading layer [==================================================>] 156.8MB/156.8MB c6e15e4ae5fb: Loading layer [==================================================>] 3.072kB/3.072kB 9ecfab6fa075: Loading layer [==================================================>] 59.9kB/59.9kB 1d75c3374e9d: Loading layer [==================================================>] 61.95kB/61.95kB Loaded image: goharbor/redis-photon:v2.3.1 8437731789fe: Loading layer [==================================================>] 6.186MB/6.186MB a28c2a8375ca: Loading layer [==================================================>] 4.096kB/4.096kB 31642a03170c: Loading layer [==================================================>] 3.072kB/3.072kB 7f18da92ac8b: Loading layer [==================================================>] 19.02MB/19.02MB 34b6ccfac5c2: Loading layer [==================================================>] 19.81MB/19.81MB Loaded image: goharbor/registry-photon:v2.3.1 c0188fb7ac5e: Loading layer [==================================================>] 6.186MB/6.186MB bf4235a4524f: Loading layer [==================================================>] 67.47MB/67.47MB 58cde91723f8: Loading layer [==================================================>] 3.072kB/3.072kB 6508007064f0: Loading layer [==================================================>] 4.096kB/4.096kB 1e3e5d3c79f5: Loading layer [==================================================>] 68.26MB/68.26MB Loaded image: goharbor/chartmuseum-photon:v2.3.1 1efde676daf0: Loading layer [==================================================>] 9.914MB/9.914MB 8d8901f3d965: Loading layer [==================================================>] 3.584kB/3.584kB 2f986b213a04: Loading layer [==================================================>] 2.56kB/2.56kB 7bae6a694788: Loading layer [==================================================>] 55.84MB/55.84MB 6ea65b302583: Loading layer [==================================================>] 5.632kB/5.632kB 5d840160fb5d: Loading layer [==================================================>] 93.7kB/93.7kB 26cb8c7aea8d: Loading layer [==================================================>] 11.78kB/11.78kB 443113e62c34: Loading layer [==================================================>] 56.74MB/56.74MB 3e1a33180139: Loading layer [==================================================>] 2.56kB/2.56kB Loaded image: goharbor/harbor-core:v2.3.1 568aa0938e2b: Loading layer [==================================================>] 8.112MB/8.112MB Loaded image: goharbor/nginx-photon:v2.3.1 be5733782dee: Loading layer [==================================================>] 9.914MB/9.914MB 9615e6a413fc: Loading layer [==================================================>] 3.584kB/3.584kB 1bd011bea638: Loading layer [==================================================>] 2.56kB/2.56kB 46add47c68f7: Loading layer [==================================================>] 62.5MB/62.5MB f931e142cd2a: Loading layer [==================================================>] 63.29MB/63.29MB Loaded image: goharbor/harbor-jobservice:v2.3.1 7aa7ae559e6f: Loading layer [==================================================>] 1.096MB/1.096MB 8aa07e284ff4: Loading layer [==================================================>] 5.888MB/5.888MB bf3312aad87c: Loading layer [==================================================>] 209.2MB/209.2MB a931ad0ebeec: Loading layer [==================================================>] 15.23MB/15.23MB 6280cae51b87: Loading layer [==================================================>] 4.096kB/4.096kB 12fba5dd3cff: Loading layer [==================================================>] 6.144kB/6.144kB 26533fac7c1e: Loading layer [==================================================>] 3.072kB/3.072kB f446e0ed5972: Loading layer [==================================================>] 2.048kB/2.048kB 7ba282a434e9: Loading layer [==================================================>] 2.56kB/2.56kB 5e2cdabb008b: Loading layer [==================================================>] 2.56kB/2.56kB e8d195e5c8a9: Loading layer [==================================================>] 2.56kB/2.56kB bdeafdbba632: Loading layer [==================================================>] 8.704kB/8.704kB Loaded image: goharbor/harbor-db:v2.3.1 94b3f2d8cdd7: Loading layer [==================================================>] 6.186MB/6.186MB efbb0d26fe83: Loading layer [==================================================>] 4.096kB/4.096kB 27ae7e20b29c: Loading layer [==================================================>] 19.02MB/19.02MB 1a981ee576a1: Loading layer [==================================================>] 3.072kB/3.072kB fe71feca4246: Loading layer [==================================================>] 25.4MB/25.4MB 46ff71a6049d: Loading layer [==================================================>] 45.2MB/45.2MB Loaded image: goharbor/harbor-registryctl:v2.3.1 8d918fd98283: Loading layer [==================================================>] 6.181MB/6.181MB 93dd6303ff3b: Loading layer [==================================================>] 6.207MB/6.207MB 3ad82d257ca6: Loading layer [==================================================>] 15.88MB/15.88MB 28412d166d6b: Loading layer [==================================================>] 29.29MB/29.29MB ecc73b87b065: Loading layer [==================================================>] 22.02kB/22.02kB 452f20807663: Loading layer [==================================================>] 15.88MB/15.88MB Loaded image: goharbor/notary-server-photon:v2.3.1 96e730f54bab: Loading layer [==================================================>] 200.5MB/200.5MB b776fbc32b13: Loading layer [==================================================>] 55.07MB/55.07MB 0c8d86f5f206: Loading layer [==================================================>] 2.56kB/2.56kB 48b6940bb86c: Loading layer [==================================================>] 1.536kB/1.536kB 8b9538516c74: Loading layer [==================================================>] 12.29kB/12.29kB 105aaabe98d9: Loading layer [==================================================>] 2.882MB/2.882MB 134aa3315fc8: Loading layer [==================================================>] 297kB/297kB Loaded image: goharbor/prepare:v2.3.1 [Step 3]: preparing environment ... [Step 4]: preparing harbor configs ... [Step 4]: preparing harbor configs ... prepare base dir is set to /soft/harbor Generated configuration file: /config/portal/nginx.conf Generated configuration file: /config/log/logrotate.conf Generated configuration file: /config/log/rsyslog_docker.conf Generated configuration file: /config/nginx/nginx.conf Generated configuration file: /config/core/env Generated configuration file: /config/core/app.conf Generated configuration file: /config/registry/config.yml Generated configuration file: /config/registryctl/env Generated configuration file: /config/registryctl/config.yml Generated configuration file: /config/db/env Generated configuration file: /config/jobservice/env Generated configuration file: /config/jobservice/config.yml Generated and saved secret to file: /data/secret/keys/secretkey Successfully called func: create_root_cert Generated configuration file: /compose_location/docker-compose.yml Clean up the input dir [Step 5]: starting Harbor ... Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating registryctl ... done Creating registry ... done Creating redis ... done Creating harbor-portal ... done Creating harbor-db ... done Creating harbor-core ... done Creating nginx ... done Creating harbor-jobservice ... done ✔ ----Harbor has been installed and started successfully.---- [root@docker36 harbor]# docker-compose ps Name Command State Ports --------------------------------------------------------------------------------------------- harbor-core /harbor/entrypoint.sh Up (healthy) harbor-db /docker-entrypoint.sh 96 13 Up (healthy) harbor-jobservice /harbor/entrypoint.sh Up (healthy) harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp harbor-portal nginx -g daemon off; Up (healthy) nginx nginx -g daemon off; Up (healthy) 0.0.0.0:80->8080/tcp redis redis-server /etc/redis.conf Up (healthy) registry /home/harbor/entrypoint.sh Up (healthy) registryctl /home/harbor/start.sh Up (healthy) [root@docker36 harbor]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f276aa2739ac goharbor/nginx-photon:v2.3.1 "nginx -g 'daemon of…" About a minute ago Up About a minute (healthy) 0.0.0.0:80->8080/tcp nginx 5904dbb33a78 goharbor/harbor-jobservice:v2.3.1 "/harbor/entrypoint.…" About a minute ago Up About a minute (healthy) harbor-jobservice 87767730bbee goharbor/harbor-core:v2.3.1 "/harbor/entrypoint.…" About a minute ago Up About a minute (healthy) harbor-core f3e47ae74126 goharbor/harbor-db:v2.3.1 "/docker-entrypoint.…" About a minute ago Up About a minute (healthy) harbor-db 453dd38da218 goharbor/redis-photon:v2.3.1 "redis-server /etc/r…" About a minute ago Up About a minute (healthy) redis 36e0f9c09c5e goharbor/harbor-registryctl:v2.3.1 "/home/harbor/start.…" About a minute ago Up About a minute (healthy) registryctl da8d6d85173d goharbor/harbor-portal:v2.3.1 "nginx -g 'daemon of…" About a minute ago Up About a minute (healthy) harbor-portal 33fdb0fde897 goharbor/registry-photon:v2.3.1 "/home/harbor/entryp…" About a minute ago Up About a minute (healthy) registry d66666462356 goharbor/harbor-log:v2.3.1 "/bin/sh -c /usr/loc…" About a minute ago Up About a minute (healthy) 127.0.0.1:1514->10514/tcp harbor-log [root@docker36 data]# ps -ef|grep post root 2179 1 0 08:58 ? 00:00:00 /usr/libexec/postfix/master -w postfix 2180 2179 0 08:58 ? 00:00:00 pickup -l -t unix -u postfix 2181 2179 0 08:58 ? 00:00:00 qmgr -l -t unix -u systemd+ 28266 28238 0 09:36 ? 00:00:00 postgres -D /var/lib/postgresql/data/pg13 -c max_connections=1024 systemd+ 28724 28266 0 09:36 ? 00:00:00 postgres: checkpointer systemd+ 28725 28266 0 09:36 ? 00:00:00 postgres: background writer systemd+ 28726 28266 0 09:36 ? 00:00:00 postgres: walwriter systemd+ 28727 28266 0 09:36 ? 00:00:00 postgres: autovacuum launcher systemd+ 28728 28266 0 09:36 ? 00:00:00 postgres: stats collector systemd+ 28729 28266 0 09:36 ? 00:00:00 postgres: logical replication launcher systemd+ 28731 28266 0 09:36 ? 00:00:00 postgres: postgres registry 172.19.0.8(52376) idle systemd+ 28734 28266 0 09:37 ? 00:00:00 postgres: postgres registry 172.19.0.8(52390) idle systemd+ 28747 28266 0 09:37 ? 00:00:00 postgres: postgres registry 172.19.0.9(51762) idle systemd+ 33306 28266 0 09:46 ? 00:00:00 postgres: postgres registry 172.19.0.8(53460) idle systemd+ 33307 28266 0 09:46 ? 00:00:00 postgres: postgres registry 172.19.0.8(53462) idle systemd+ 33308 28266 0 09:46 ? 00:00:00 postgres: postgres registry 172.19.0.8(53464) idle systemd+ 33309 28266 0 09:46 ? 00:00:00 postgres: postgres registry 172.19.0.8(53466) idle root 38433 9653 0 09:57 pts/1 00:00:00 grep --color=auto post |
可以看到Harbor已经成功安装并启动了。
数据库包含PostgreSQL和Redis。
web使用Harbor
1 | Windows环境hosts文件位置:C:\Windows\System32\drivers\etc\hosts |
访问 https://www.xmmharbor.com 或 https://192.168.1.36 ,如下:
用户名密码为:admin/Harbor12345
可以创建项目,创建用户,给项目分配用户等等,操作都很简单 。
当项目设为公开后,任何人都有此项目下镜像的读权限。命令行用户不需要“docker login”就可以拉取此镜像。
客户端上传镜像
首先登录私有仓库,可以使用 admin 用户 ,也可以使用我们自己创建的具有上传权限的用户:
1 | docker login -u admin -p Harbor12345 www.xmmharbor.com |
如果报错:
Error response from daemon: Get https://www.xmmharbor.com/v2/: unauthorized: authentication required
,原因:这是密码输入错误了。。。。如果报错:
1 2 3 | [root@docker35 docker]# docker login -u admin -p Harbor12345 www.xmmharbor.com WARNING! Using --password via the CLI is insecure. Use --password-stdin. Error response from daemon: Get https://www.xmmharbor.com/v2/: x509: certificate signed by unknown authority |
解决方法一:将服务器端的/etc/docker/certs.d
文件夹拷贝到客户端的相同位置:
1 | scp -r /etc/docker/certs.d 192.168.1.35:/etc/docker/ |
然后重启docker即可。
解决方法二:则需要修改客户端的/etc/docker/daemon.json
文件,增加如下内容:
1 | "insecure-registries": ["www.xmmharbor.com"] |
例如:
1 2 3 4 5 6 7 8 9 10 11 | [root@docker35 docker]# more daemon.json { "registry-mirrors": [ "https://hub.daocloud.io", "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn", "https://vm1wbfhf.mirror.aliyuncs.com", "https://pee6w651.mirror.aliyuncs.com" ], "insecure-registries": ["www.xmmharbor.com"] } |
然后重启docker!!!
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 44 45 46 47 48 49 50 51 52 53 54 55 56 | [root@docker35 docker]# systemctl restart docker [root@docker35 docker]# docker info Client: Debug Mode: false Server: Containers: 152 Running: 15 Paused: 0 Stopped: 137 Images: 133 Server Version: 19.03.8 Storage Driver: overlay2 Backing Filesystem: <unknown> Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-1127.10.1.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 8 Total Memory: 44.86GiB Name: docker35 ID: 3IXQ:YJEX:UU5M:UAOA:6LD4:ZVEQ:B5CT:XIT7:LN2B:H56N:E3RB:74DT Docker Root Dir: /var/lib/docker Debug Mode: false Username: lhrbest Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: www.xmmharbor.com 127.0.0.0/8 Registry Mirrors: https://hub.daocloud.io/ https://hub-mirror.c.163.com/ https://docker.mirrors.ustc.edu.cn/ https://vm1wbfhf.mirror.aliyuncs.com/ https://pee6w651.mirror.aliyuncs.com/ Live Restore Enabled: false |
可以看到Insecure Registries的值已经被修改了。
要通过docker tag将该镜像标志为要推送到公有仓库,例如:
1 | docker tag nginx:latest www.xmmharbor.com/xmmtestpub/nginx:latest |
上传镜像:
1 2 3 4 5 6 7 8 | [root@docker35 docker]# docker push www.xmmharbor.com/xmmtestpub/nginx:latest The push refers to repository [www.xmmharbor.com/xmmtestpub/nginx] f978b9ed3f26: Pushed 9040af41bb66: Pushed 7c7d7f446182: Pushed d4cf327d8ef5: Pushed 13cb14c2acd3: Pushed latest: digest: sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f size: 1362 |
上传完成。
访问 web界面https://www.xmmharbor.com/harbor/projects,刷新:
可以下载:
1 2 3 4 5 6 7 8 9 10 | [root@docker36 ~]# docker pull www.xmmharbor.com/xmmtestpub/nginx@sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f: Pulling from xmmtestpub/nginx 8559a31e96f4: Pull complete 8d69e59170f7: Pull complete 3f9f1ec1d262: Pull complete d1f5ff4f210d: Pull complete 1e22bfa8652e: Pull complete Digest: sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f Status: Downloaded newer image for www.xmmharbor.com/xmmtestpub/nginx@sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f www.xmmharbor.com/xmmtestpub/nginx@sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f |