🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 简介 Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。 ## 安装环境准备 注意:Harbor的所有服务组件都是在Docker中部署的,所以官方安装使用Docker-compose快速部署,所以我们需要安装Docker、Docker-compose。由于Harbor是基于Docker Registry V2版本,所以就要求Docker版本不小于1.10.0,Docker-compose版本不小于1.6.0。 ## 安装Harbor ``` $ mkdir /data/ $ cd /data/ $ wget https://storage.googleapis.com/harbor-releases/release-1.9.0/harbor-offl ine-installer-v1.9.4.tgz $ tar xvzf harbor-offline-installer-v1.9.4.tgz $ cd harbor $ vi harbor.yml # 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. # 自行更改为服务器内部ip hostname: 192.168.0.3 $ ./install.sh ``` 注意:若wget无法下载,请先在开发机访问:https://storage.googleapis.com/harbor-releases/release-1.9.0/harbor-offline-installer-v1.9.4.tgz。下载完毕后传至服务器即可 若需要修改配置: ``` $ docker-compose down -v $ vi harbor.yml $ ./prepare $ docker-compose up -d ``` 重启: ``` $ docker-compose stop $ docker-compose start ``` ## 登录harbor 地址: http://192.168.0.3 用户名: admin 密码: Harbor12345 ## 测试推送 1. admin登录: ``` $ docker login 192.168.0.3 Username: admin Password: Login Succeeded ``` 2. 测试给镜像打tag ``` $ docker tag system-server 192.168.0.3/jpower/system-server:latest $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE system-server latest 958a7ae9e569 1 weeks ago 109 MB 192.168.0.3/jpower/system-server latest 958a7ae9e569 1 weeks ago 109 MB ``` 3. push到仓库 ``` $ docker push 192.168.0.3/jpower/system-server The push refers to a repository [192.168.0.3/jpower/system-server] a552ca691e49: Pushed 7487bf0353a7: Pushed 8781ec54ba04: Pushed latest: digest: sha256:41ad9967ea448d7c2b203c699b429abe1ed5af331cd92533900c6d77 490e0268 size: 948 ``` ## 开放2375端口 注意:在外网开放有安全风险,只推荐在内网对外开放 `vi /lib/systemd/system/docker.service ` 在[Service]节点下方增加 ``` ExecStart= ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -Hunix://var/run/docker.sock ``` ``` $ systemctl daemon-reload $ systemctl restart docker $ docker-compose stop $ docker-compose start $ firewall-cmd --add-port=2375/tcp --permanent $ service firewalld restart ``` ## maven配置 * 在maven的settings.xml配置文件中加入以下配置 settings中加入: ``` <pluginGroups> <pluginGroup>com.spotify</pluginGroup> </pluginGroups> ``` servers中加入: ``` <server> <id>192.168.0.3</id> <username>admin</username> <password>Harbor12345</password> <configuration> <email>ding931226@yeah.net</email> </configuration> </server> ``` * 在工程中修改最上级pom.xml文件 ![](https://img.kancloud.cn/b9/4a/b94a41eb605d020b1fec98addf644386_1105x1162.png) 将截图中的配置修改为: ~~~ <docker.registry>192.168.0.3</docker.registry> ~~~ ## FQA 1. 配置并启动Harbor之后,本地执行登录操作,报错: ``` $ docker login 192.168.0.3 Username: admin Password: Error response from daemon: Get https://192.168.0.3/v1/users/: dial tcp 192.168.0.3:443: getsockopt: connection refused ``` 这是因为docker1.3.2版本开始默认docker registry使用的是https,我们设置Harbor默认http方式,所以当执行用docker login、pull、push等命令操作非https的docker regsitry的时就会报错。 解决办法:配置 /etc/docker/daemon.json ``` [root@localhost harbor]# vi /etc/docker/daemon.json { "registry-mirrors": ["https://3dse7md.mirror.aliyuncs.com"] } ``` 将其修改为: ``` { "registry-mirrors": ["https://3dse7md.mirror.aliyuncs.com"], "insecure-registries": ["192.168.0.3"] } ``` 重启 ``` $ systemctl daemon-reload && systemctl restart docker ``` 2. 使用非admin账户执行docker pull命令时,报错 ``` $ docker pull 192.168.0.3/jpower/system-server:latest Error response from daemon: repository 192.168.0.3/jpower/system-server not found: does not exist or no pull access ``` 这是原因可能有: 一是Harbor仓库上/jpower/system-server确实不存在, 二是项目未设置公开的时候,该账户未执行docker login 192.168.0.3登录操作, 三是该账户对192.168.0.3/jpower项目没有权限,需要在该项目下增加jpower成员,并选择角色。 3. 如果需要修改Harbor的配置文件harbor.cfg,因为Harbor是基于docker-compose服务编排的,我们可以使用docker-compose命令重启Harbor。不修改配置文件,重启Harbor命令:docker-compose start | stop | restart ``` //停止Harbor $ docker-compose down -v Stopping nginx ... done Stopping harbor-jobservice ... done ...... Removing harbor-log ... done Removing network harbor_harbor //启动Harbor $ docker-compose up -d Creating network "harbor_harbor" with the default driver Creating harbor-log ... ...... Creating nginx Creating harbor-jobservice ... done ```