使用Harbor搭建局域网私有docker镜像库

说明:本文只介绍使用http的方式访问Harbor镜像库,https访问的方式请自行查询。

一、安装harbo

前提条件:已安装好docker。

  • 1.1、安装docker-compose

    bash 复制代码
    sudo apt install docker-compose
  • 1.2、下载Harbor

    Harbor的官方github,下载最新的版本。注意最好下载离线版本harbor-offline-installer-vx.xx.x.tgz。在线安装对网络要求高,一般国内由于某些原因无法在线安装。

  • 1.3、安装Harbor

    我这里以harbor-offline-installer-v2.11.0.tgz这个版本为例。

    • 1.3.1、解压
    bash 复制代码
    tar -vxzf harbor-offline-installer-v2.11.0.tgz

    解压后会得到一个harbor文件夹。

    • 1.3.2、配置文件
    bash 复制代码
    cd harbor
    cp harbor.yml.tmpl harbor.yml
    • 1.3.3、修改harbor.yml
    bash 复制代码
    vim harbor.yml

    配置主机和端口:

    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.
    hostname: 改为自己的主机名(通过命令 cat /etc/hostname 获取)
    
    # http related config
    http:
    # port for http, default is 80. If https enabled, this port will redirect to https port
    	port: 8180 #这里默认是80,如果80端口已占用改为其他端口

    屏蔽https相关的配置,以后需要再配置:

    yml 复制代码
    # https related config
    # https:
    	# https port for harbor, default is 443
    	# port: 443
    	# The path of cert and key files for nginx
    	# certificate: /your/certificate/path
    	# private_key: /your/private/key/path
    	# enable strong ssl ciphers (default: false)
    	# strong_ssl_ciphers: false

    配置harbor的存储位置:

    yml 复制代码
    # The default data volume
    data_volume: /home/zhangge/data/harbor_data  # 默认是/data,改为自己想存放的位置

    配置harbor的log存储位置:

    yml 复制代码
    # Log configurations
    log:
    	# options are debug, info, warning, error, fatal
    	level: info
    	# configs for logs in local storage
    	local:
    		# Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated.
    		rotate_count: 50
    		# Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes.
    		# If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G
    		# are all valid.
    		rotate_size: 200M
    		# The directory on your host that store log
    		location: /home/zhangge/data/harbor_data/log/harbor #改为自己想存放的路径
    • 1.3.4、导入使用到的docker镜像
    bash 复制代码
    # 在harbor文件夹中会有harbor.v2.11.0.tar.gz这个文件
    docker load -i harbor.v2.11.0.tar.gz
    • 1.3.5、开始安装
    bash 复制代码
    ./prepare
    sudo ./install.sh
    • 1.3.6、需要重新更改配置时

    停止harbor启动的容器:

    bash 复制代码
    cd harbor
    sudo docker-compose down -v
    
    # 修改完配置后进行重新安装

    开机后如果服务没起来:

    bash 复制代码
    cd harbor
    sudo docker-compose up -d

二、harbo的web界面

在浏览器输入http://127.0.0.1:8180,端口改为配置的端口。可以进入web访问界面:

默认用户名:admin,密码:Harbor12345。输入后可访问管理页面。

三、docker配置http访问

docker默认是https访问,需要配置为支持http访问。

bash 复制代码
sudo vim /etc/docker/daemon.json

添加以下内容:

json 复制代码
{
        "insecure-registries": ["10.0.3.152:8180"]
}

其中,需要把ip改为自己的主机ip。

重启docker守护进程:

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl restart docker

四、docker使用Harbor

Harbor默认创建了library这个项目,以向library项目推送、拉取为例:

我这里的主机ip为: 10.0.3.152,配置的端口为8180。

  • 4.1、登录Harbor

    bash 复制代码
    docker login http://10.0.3.152:8180
    
    # 然后输入Harbor的用户名和密码
  • 4.2、推送仓库

    bash 复制代码
    # 将从docker-hub拉取的ubuntu:20.04仓库打个tag:
    docker tag ubuntu:20.04 10.0.3.152:8180/library/ubuntu:20.04
    
    # 推送到Harbor仓库
    docker push 10.0.3.152:8180/library/ubuntu:20.04

    推送完后,也可以登录管理界面进行查看。

  • 4.3、拉取仓库

    bash 复制代码
    # 本机或局域网内其他主机
    docker pull 10.0.3.152:8180/library/ubuntu:20.04

五、WSL下使用Harbor

WSL中使用docker harbor私服拉取镜像时,报如下错误:

root@node02 ~]# docker login http://10.0.3.152:8180
Username: admin
Password:
Error response from daemon: Get https://10.0.3.152:8180/v2/: Get https://zhangge-OptiPlex-3080/service/token?account=admin\&client_id=docker\&offline_token=true\&service=harbor-registry: dial tcp: lookup harbor on 10.0.3.152:53: no such host

因为在拉取镜像时会调用harbor服务接口,这里因找不到zhangge-OptiPlex-3080域名报错,在hosts文件中增加域名映射即可:

bash 复制代码
sudo vi /etc/hosts

添加如下内容:

txt 复制代码
10.0.3.152      zhangge-OptiPlex-3080

将ip后面的主机名改为实际的主机名,接下来就正常了。

相关推荐
C++忠实粉丝23 分钟前
Linux环境基础开发工具使用(2)
linux·运维·服务器
康熙38bdc1 小时前
Linux 环境变量
linux·运维·服务器
存储服务专家StorageExpert1 小时前
DELL SC compellent存储的四种访问方式
运维·服务器·存储维护·emc存储
大G哥2 小时前
记一次K8S 环境应用nginx stable-alpine 解析内部域名失败排查思路
运维·nginx·云原生·容器·kubernetes
醉颜凉2 小时前
银河麒麟桌面操作系统修改默认Shell为Bash
运维·服务器·开发语言·bash·kylin·国产化·银河麒麟操作系统
大道归简3 小时前
Docker 命令从入门到入门:从 Windows 到容器的完美类比
windows·docker·容器
苦逼IT运维3 小时前
YUM 源与 APT 源的详解及使用指南
linux·运维·ubuntu·centos·devops
仍有未知等待探索3 小时前
Linux 传输层UDP
linux·运维·udp
zeruns8023 小时前
如何搭建自己的域名邮箱服务器?Poste.io邮箱服务器搭建教程,Linux+Docker搭建邮件服务器的教程
linux·运维·服务器·docker·网站
爱跑步的程序员~3 小时前
Docker
docker·容器