Harbor安装与使用

本文最后更新于 2025年2月21日 凌晨

Harbor 是一个开源的可信云原生注册表项目,可用于存储和分发 Docker 镜像。本文将介绍如何在 Ubuntu 系统上安装和配置 Harbor,以及如何使用 Harbor 进行镜像的推送和拉取。

1 环境准备

1.1硬件要求

资源 最低配置 推荐配置
CPU 2 核 4 核
内存 4 GB 8 GB
磁盘 40 GB 160 GB

1.2软件要求

  • 操作系统:Ubuntu 20.04 或更高版本
  • Docker:20.10.10 以上版本
  • Docker Compose:v1.18.0 以上版本,或 Docker Compose v2(docker-compose-plugin
  • OpenSSL:默认已安装,可以通过openssl version查看

安装 Docker

  1. 更新 apt 包索引

    1
    sudo apt update
  2. 安装必要的依赖

    1
    2
    3
    4
    5
    6
    sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  3. 拉取GPG密钥

    1
    2
    3
    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
  4. 添加源

    1
    echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  5. 更新 apt 包索引并安装 Docker

    1
    2
    sudo apt update   
    sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  6. 查看安装版本

    1
    2
    3
    4
    docker -v
    Docker version 28.0.0
    docker compose version
    Docker Compose version v2.33.0

1.3端口

harbor 要求目标主机开放以下端口,如果使用反代,后面是可以通过配置修改这些端口号的。

端口 协议 描述
443 HTTPS Harbor门户和核心API在此端口接受HTTPS请求。可以在配置文件中更改此端口。
4443 HTTPS Harbor的Docker内容信任服务连接。可以在配置文件中更改此端口。
80 HTTP Harbor门户和核心API在此端口接受HTTP请求。可以在配置文件中更改此端口。

2 安装Harbor

2.1下载 Harbor

从 Harbor 发布页下载离线安装包:harbor-offline-installer-v2.12.2.tgz。由于文件较大,建议先下载到本地,再上传至服务器。

1
2
3
wget https://github.com/goharbor/harbor/releases/download/v2.12.2/harbor-offline-installer-v2.12.2.tgz

tar xzvf harbor-offline-installer-v2.12.2.tgz

2.2修改配置文件

在软件目录下

1
2
cp harbor.yml.tmpl harbor.yml
vim harbor.yml

我这里直接使用Harbor的nginx,并且使用https,所以需求进行下列修改:

* hostname: reg.mydomain.com改成要使用的域名
* certificate: /root/https/mydomain.pem
* private_key: /root/https/mydomain.key

如果要用外面的nginx做反代,则需要修改

  • port
  • external_url

2.3安装

在 Harbor 目录下执行:

1
./install

安装日志示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Step 5]: starting Harbor ...
[+] Running 10/10
✔ Network harbor_harbor Created 0.0s
✔ Container harbor-log Started 0.4s
✔ Container registry Started 0.6s
✔ Container registryctl Started 0.6s
✔ Container harbor-portal Started 0.6s
✔ Container harbor-db Started 0.6s
✔ Container redis Started 0.6s
✔ Container harbor-core Started 0.8s
✔ Container harbor-jobservice Started 0.9s
✔ Container nginx Started 1.0s

安装完成后,通过域名访问。默认用户名和密码是 admin 和 Harbor12345。

3 使用

3.1用户管理

在 系统管理 -> 用户管理 界面创建用户。用户分为管理员和普通用户,默认创建为普通用户。

3.2项目管理

项目分为 私有 和 公开。公开项目允许所有用户读取,私有项目仅限特定用户访问。可以在项目的成员列表中设置访问权限。

3.3推送镜像

首先需要登录:

1
2
3
4
5
6
7
8
9
docker login <Harbor服务器地址>
# 例如
docker login harbor.mycompany.com

Username: test
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

然后给想推送的镜像打TAG,标签格式:

1
<Harbor服务器地址>/<项目名称>/<镜像名称>:<标签>

实际命令示例:

1
docker tag python:3.10-sli harbor.mycompany.com/library/python:3.10-slim

如果是自编则就直接加上地址:

1
docker build -t harbor.mycompany.com/library/python:3.10-slim .

推送:

1
2
3
4
5
6
7
docker push harbor.mycompany.com/library/python:3.10-slim

46b43d63df64: Pushed
f82a34d9ac9e: Pushed
228f3372b10a: Pushed
8b296f486960: Pushed
3.10-slim: digest: sha256:65461331c9562d3ec474b541ede343be22d616923493ef54c953a30a477b343a size: 1159

直接在web页面中,public项目里的镜像仓库页面便能看到刚上传的镜像了

3.4拉取镜像

仍然需要先登录

1
2
docker login <Harbor服务器地址>
docker login harbor.mycompany.com

然后拉取

1
2
3
4
5
6
7
8
9
10
11
docker pull <Harbor服务器地址>/<项目名称>/<镜像名称>:<标签>
docker pull harbor.mycompany.com/public/python:3.10-slim

3.10-slim: Pulling from public/python
8a47f6aad193: Pull complete
a1235d039a7d: Pull complete
e17464c8c9fb: Pull complete
f344618db07e: Pull complete
Digest: sha256:65461331c123453ec474b541ede343be22d61691f493ef54c953a30a477b343a
Status: Downloaded newer image for harbor.mycompany.com/public/python:3.10-slim
harbor.mycompany.com/public/python:3.10-slim

3.5批量推送脚本

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
"""
使用说明

1. 登录harbor
sudo docker login harbor.mycompany.com
chmod +x push_images.py
python test.py
"""

import subprocess

# 定义要处理的镜像列表
images = [
"openjdk:21-ea-17-slim-buster",
"hyperf/hyperf:8.1-alpine-v3.15-swoole",
"hyperf/hyperf:8.1-alpine-v3.16-swoole-v5",
"yandex/clickhouse-server:latest",
"elasticsearch:8.9.0",
"kibana:8.9.0",
"mysql:latest",
"node:18.20.1-alpine3.19",
"node:18.20.5-alpine3.21",
"node:14.20.1-alpine3.16",
"elasticsearch:8.14.3",
"kibana:8.14.3",
"mongo:5.0.5",
"emqx:5.1.3",
"influxdb:2.0.9",
"mongo:5.0.23",
"xuxueli/xxl-job-admin:2.4.0"
]

# 目标仓库前缀
registry_prefix = "harbor.mycompany.com/public/"

for image in images:
# 构建新的镜像名称
new_image = f"{registry_prefix}{image}"

try:
# 拉取镜像
print(f"正在拉取镜像 {image}...")
subprocess.run(["sudo", "docker", "pull", image], check=True)

# 为镜像打标签
print(f"正在为镜像 {image} 打标签为 {new_image}...")
subprocess.run(["sudo", "docker", "tag", image, new_image], check=True)

# 推送到目标仓库
print(f"正在推送镜像 {new_image}...")
subprocess.run(["sudo", "docker", "push", new_image], check=True)

# 可选:删除本地的中间镜像(如果不需要保留)
# subprocess.run(["sudo", "docker", "rmi", image], check=True)
# subprocess.run(["sudo", "docker", "rmi", new_image], check=True)

except subprocess.CalledProcessError as e:
print(f"处理镜像 {image} 时发生错误:{e}")
continue

print("所有镜像已完成拉取、标签和推送。")

4 相关链接


Harbor安装与使用
https://blog.kala.love/posts/9929d428/
作者
久远·卡拉
发布于
2025年2月21日
许可协议