使用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后面的主机名改为实际的主机名,接下来就正常了。

相关推荐
宋康5 分钟前
Docker 常用命令
docker·容器·eureka
SHUIPING_YANG8 分钟前
Nginx 返回 504 状态码表示 网关超时(Gateway Timeout)原因排查
运维·nginx·gateway
光不度AoKaNa20 分钟前
计算机操作系统概要
linux·运维·服务器
晚秋大魔王26 分钟前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——wget
java·linux·运维·开发语言·华为·harmonyos
孤的心了不冷1 小时前
【Linux】Linux安装并配置MongoDB
linux·运维·mongodb·容器
南棱笑笑生1 小时前
20250517让NanoPi NEO core开发板在Ubuntu core16.04.2下支持TF卡的热插拔
linux·运维·ubuntu
jinlei20092 小时前
配置ssh服务-ubuntu到Windows拷贝文件方法
运维·ubuntu·ssh
magic 2452 小时前
第6章:文件权限
linux·运维·服务器
c6lala2 小时前
数据结构day3
linux·运维·服务器
snpgroupcn3 小时前
天能股份SAP系统整合实战:如何用8个月实现零业务中断的集团化管理升级
运维·系统架构