自学内容网 自学内容网

Docker--harbor私有仓库部署与管理

前言

首先安装docker可以看下面教程

docker安装教程

一、搭建本地私有仓库

1、下载registry 镜像

 docker pull registry
[root@VM-8-7-centos docker]#  docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete 
0d96da54f60b: Pull complete 
5b27040df4a2: Pull complete 
e2ead8259a04: Pull complete 
3790aef225b9: Pull complete 
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@VM-8-7-centos docker]# 
[root@VM-8-7-centos docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
registry     latest    b8604a3fe854   2 years ago   26.2MB
[root@VM-8-7-centos docker]# 

2、修改 daemon.json

vim /etc/docker/daemon.json

 "insecure-registries":["http://43.000.00.02:5000"]

修改后重启docker使配置生效

docker version # 查看Docker版本信息

systemctl start docker# 启动 docker 服务:
systemctl stop docker# 停止 docker 服务:
systemctl status docker# 查看 docker 服务状态
systemctl restart docker# 重启 docker 服务

3、启动registry容器

#-itd:在容器中打开一个伪终端进行交互操作,并在后台运行
#-v:把宿主机的/data/registry目录绑定到容器/var/lib/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现数据的持久化;
#-p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了
#--restart=always:这是重启的策略,在容器退出时总是重启容器
#--name registry:创建容器命名为registry
#registry:latest:这个是刚才pull下来的镜像

Docker容器的重启策略如下:
no:默认策略,在容器退出时不重启容器
on-failure:在容器非正常退出时(退出状态非0),才会重启容器
on-failure:3 :在容器非正常退出时重启容器,最多重启3次
always:在容器退出时总是重启容器
unless-stopped:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器 

docker run -d -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest

4、上传镜像到本地私有仓库 

这里做拉取nginx镜像并为该镜像打新标签进行演示

 4.1、拉取镜像打上新标签
 

#为该镜像添加新的标签
docker tag nginx:latest 43.143.249.82:5000/nginx:v1
# 43.143.249.82:5000/nginx:v1: 这是新的标签,它包含了两部分信息。43.143.249.82:5001 是 Registry 的地址,表示你要将这个镜像推送到指定的 Registry。nginx:v1 则是新的标签,表示将原本的 nginx:latest 镜像打上一个新的标签为 nginx:v1。
[root@VM-8-7-centos docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@VM-8-7-centos docker]# 
[root@VM-8-7-centos docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    605c77e624dd   2 years ago   141MB
registry     latest    b8604a3fe854   2 years ago   26.2MB
[root@VM-8-7-centos docker]# 
[root@VM-8-7-centos docker]# docker tag nginx:latest 43.143.249.82:5000/nginx:v1
[root@VM-8-7-centos docker]# 
[root@VM-8-7-centos docker]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED       SIZE
43.143.249.82:5000/nginx   v1        605c77e624dd   2 years ago   141MB
nginx                      latest    605c77e624dd   2 years ago   141MB
registry                   latest    b8604a3fe854   2 years ago   26.2MB
[root@VM-8-7-centos docker]# 

4.2、上传至私有仓库 

#将本地的镜像 43.143.249.82:5000/nginx:v1 推送(上传)到 Registry 地址 43.143.249.82:5000 中

docker push 43.143.249.82:5000/nginx:v1

4.3、列出私有仓库

#列出私有仓库的所有镜像
[root@VM-8-7-centos docker]# curl http://43.143.249.82:5000/v2/_catalog
{"repositories":["nginx"]}

#列出私有仓库的 nginx 镜像的所有 tag
[root@VM-8-7-centos docker]# curl http://43.143.249.82:5000/v2/nginx/tags/list
{"name":"nginx","tags":["v1"]}
[root@VM-8-7-centos docker]# 

4.4、验证私有仓库

#删除原有的 nginx:v1 的镜像
 docker rmi 43.143.249.82:5000/nginx:v1

#从私有仓库下载 nginx:v1 镜像
docker pull 43.143.249.82:5000/nginx:v1

二、Docker--harbor私有仓库部署

1、部署 Docker-Compose 服务

docker安装已经有了不需在单独装了docker-compose,直接找到文件位置移动就行了

#找文件位置
find / -name "*compose"
#复制到/usr/local/bin/
cp /usr/libexec/docker/cli-plugins/docker-compose /usr/local/bin/
#版本号
docker-compose -v

[root@VM-8-7-centos docker]# find / -name "*compose"
/usr/libexec/docker/cli-plugins/docker-compose
[root@VM-8-7-centos docker]# 
[root@VM-8-7-centos docker]# cp /usr/libexec/docker/cli-plugins/docker-compose /usr/local/bin/
[root@VM-8-7-centos docker]# docker-compose -v
Docker Compose version v2.27.1
[root@VM-8-7-centos docker]# 

2、下载 Harbor 安装程序

harbor下载地址

Harbor安装包分为在线和离线安装包两个如下所示

下载
wget https://github.com/goharbor/harbor/releases/download/v1.10.18/harbor-offline-installer-v1.10.18.tgz
解压到/usr/local/目录下
tar -zxvf harbor-offline-installer-v1.10.18.tgz -C /usr/local/

 本人采用直接在Github上下载好上传到服务器

[root@VM-8-7-centos local]# ls
bin   etc       games                                  imagemagick  java-jdk8  lib64    memcached  nginx  php       qcloud       sbin   src
curl  freetype  harbor-offline-installer-v1.10.18.tgz  include      lib        libexec  mysql      pgsql  pureftpd  redis-7.0.5  share
[root@VM-8-7-centos local]# 
[root@VM-8-7-centos local]# 
[root@VM-8-7-centos local]# tar -zxvf harbor-offline-installer-v1.10.18.tgz
harbor/harbor.v1.10.18.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml
[root@VM-8-7-centos local]# ls
bin   etc       games   harbor-offline-installer-v1.10.18.tgz  include    lib    libexec    mysql  pgsql  pureftpd  redis-7.0.5  share
curl  freetype  harbor  imagemagick                            java-jdk8  lib64  memcached  nginx  php    qcloud    sbin         src
[root@VM-8-7-centos local]# cd harbor/
[root@VM-8-7-centos harbor]# ll
总用量 520376
-rw-r--r-- 1 root root      3398 5月   8 2023 common.sh
-rw-r--r-- 1 root root 532828531 5月   8 2023 harbor.v1.10.18.tar.gz
-rw-r--r-- 1 root root      5882 5月   8 2023 harbor.yml
-rwxr-xr-x 1 root root      2284 5月   8 2023 install.sh
-rw-r--r-- 1 root root     11347 5月   8 2023 LICENSE
-rwxr-xr-x 1 root root      1750 5月   8 2023 prepare
[root@VM-8-7-centos harbor]#

3、修改 harbor 安装的配置文件

#编辑
vim harbor.yml

4、启动Harbor

#可以不执行
./prepare
#安装
./install.sh

 启动命令 docker-compose up -d

报错一:✘ Container 22db908c396f_registryctl  Error while Removing

处理方式:删除22db908c396f_registryctl这个容器

启动报错处理
[root@VM-8-7-centos harbor]# docker-compose up -d
[+] Running 1/0
 ⠋ Container registry                  Creating                                                                                                     0.0s 
 ✘ Container 22db908c396f_registryctl  Error while Removing                                                                                         0.0s 
 ⠋ Container registryctl               Recreate                                                                                                     0.0s 
Error response from daemon: Conflict. The container name "/registry" is already in use by container "f2c4da9f75a2d2146ce948379095fdc54d56ad51d29a6f0211f68ad793fc890b". You have to remove (or rename) that container to be able to reuse that name.
[root@VM-8-7-centos harbor]# 
[root@VM-8-7-centos harbor]# docker ps -a
CONTAINER ID   IMAGE                                  COMMAND                   CREATED         STATUS          PORTS                                       NAMES
3d7da957382f   goharbor/harbor-log:v1.10.18           "/bin/sh -c /usr/loc…"   4 minutes ago   Created                                                     harbor-log
b23fe5d5d2e5   goharbor/harbor-db:v1.10.18            "/docker-entrypoint.…"   2 hours ago     Created                                                     harbor-db
22db908c396f   goharbor/harbor-registryctl:v1.10.18   "/home/harbor/start.…"   2 hours ago     Created                                                     registryctl
aa0ef6952b60   goharbor/redis-photon:v1.10.18         "redis-server /etc/r…"   2 hours ago     Created                                                     redis
715e9bb0ead6   goharbor/harbor-portal:v1.10.18        "nginx -g 'daemon of…"   2 hours ago     Created                                                     harbor-portal
f2c4da9f75a2   registry:latest                        "/entrypoint.sh /etc…"   20 hours ago    Up 38 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@VM-8-7-centos harbor]# docker rm 22db908c396f
22db908c396f
[root@VM-8-7-centos harbor]# docker ps -a
CONTAINER ID   IMAGE                             COMMAND                   CREATED         STATUS              PORTS                                       NAMES
3d7da957382f   goharbor/harbor-log:v1.10.18      "/bin/sh -c /usr/loc…"   4 minutes ago   Created                                                         harbor-log
b23fe5d5d2e5   goharbor/harbor-db:v1.10.18       "/docker-entrypoint.…"   2 hours ago     Created                                                         harbor-db
aa0ef6952b60   goharbor/redis-photon:v1.10.18    "redis-server /etc/r…"   2 hours ago     Created                                                         redis
715e9bb0ead6   goharbor/harbor-portal:v1.10.18   "nginx -g 'daemon of…"   2 hours ago     Created                                                         harbor-portal
f2c4da9f75a2   registry:latest                   "/entrypoint.sh /etc…"   20 hours ago    Up About a minute   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@VM-8-7-centos harbor]# 

registry这是容器是最开始1.3中启动得容器

报错二:Error response from daemon: Conflict. The container name "/registry" is already in use by container "f2c4da9f75a2d2146ce948379095fdc54d56ad51d29a6f0211f68ad793fc890b". You have to remove (or rename) that container to be able to reuse that name.

处理方式:先停止容器在删除容器 如下:

[root@VM-8-7-centos harbor]# docker ps
CONTAINER ID   IMAGE             COMMAND                   CREATED        STATUS         PORTS                                       NAMES
f2c4da9f75a2   registry:latest   "/entrypoint.sh /etc…"   20 hours ago   Up 2 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@VM-8-7-centos harbor]# docker stop f2c4da9f75a2
f2c4da9f75a2
[root@VM-8-7-centos harbor]# docker ps -a
CONTAINER ID   IMAGE                                  COMMAND                   CREATED         STATUS                      PORTS     NAMES
770eb6378188   goharbor/harbor-registryctl:v1.10.18   "/home/harbor/start.…"   2 minutes ago   Created                               registryctl
3d7da957382f   goharbor/harbor-log:v1.10.18           "/bin/sh -c /usr/loc…"   7 minutes ago   Created                               harbor-log
b23fe5d5d2e5   goharbor/harbor-db:v1.10.18            "/docker-entrypoint.…"   2 hours ago     Created                               harbor-db
aa0ef6952b60   goharbor/redis-photon:v1.10.18         "redis-server /etc/r…"   2 hours ago     Created                               redis
715e9bb0ead6   goharbor/harbor-portal:v1.10.18        "nginx -g 'daemon of…"   2 hours ago     Created                               harbor-portal
f2c4da9f75a2   registry:latest                        "/entrypoint.sh /etc…"   20 hours ago    Exited (2) 16 seconds ago             registry
[root@VM-8-7-centos harbor]# docker rm f2c4da9f75a2
f2c4da9f75a2

再次后台启动 docker-compose up -d 还有2个不正常见登录

全部启动成功后,如图:查看harbor相关容器启动情况(总计9个):

[root@VM-8-7-centos harbor]# docker-compose up -d
[+] Running 9/9
 ✔ Container registry           Started                                                                                                             1.4s 
 ✔ Container harbor-core        Started                                                                                                             1.9s 
 ✔ Container nginx              Started                                                                                                             2.5s 
 ✔ Container harbor-jobservice  Started                                                                                                             2.5s 
 ✔ Container harbor-log         Started                                                                                                             0.3s 
 ✔ Container harbor-db          Started                                                                                                             0.5s 
 ✔ Container redis              Started                                                                                                             0.7s 
 ✔ Container registryctl        Started                                                                                                             0.8s 
 ✔ Container harbor-portal      Started                                                                                                             0.6s 
[root@VM-8-7-centos harbor]# docker ps 
CONTAINER ID   IMAGE                                  COMMAND                   CREATED          STATUS                             PORTS                                       NAMES
dc0dbe3e6407   goharbor/harbor-jobservice:v1.10.18    "/harbor/harbor_jobs…"   19 seconds ago   Up 16 seconds (health: starting)                                               harbor-jobservice
de4debd5fe43   goharbor/nginx-photon:v1.10.18         "nginx -g 'daemon of…"   19 seconds ago   Up 16 seconds (health: starting)   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   nginx
069b20d30b15   goharbor/harbor-core:v1.10.18          "/harbor/harbor_core"     19 seconds ago   Up 16 seconds (health: starting)                                               harbor-core
144c7c124bd0   goharbor/registry-photon:v1.10.18      "/home/harbor/entryp…"   19 seconds ago   Up 17 seconds (health: starting)   5000/tcp                                    registry
770eb6378188   goharbor/harbor-registryctl:v1.10.18   "/home/harbor/start.…"   2 minutes ago    Up 17 seconds (health: starting)                                               registryctl
3d7da957382f   goharbor/harbor-log:v1.10.18           "/bin/sh -c /usr/loc…"   7 minutes ago    Up 18 seconds (health: starting)   127.0.0.1:1514->10514/tcp                   harbor-log
b23fe5d5d2e5   goharbor/harbor-db:v1.10.18            "/docker-entrypoint.…"   2 hours ago      Restarting (1) 1 second ago                                                    harbor-db
aa0ef6952b60   goharbor/redis-photon:v1.10.18         "redis-server /etc/r…"   2 hours ago      Restarting (1) 1 second ago                                                    redis
715e9bb0ead6   goharbor/harbor-portal:v1.10.18        "nginx -g 'daemon of…"   2 hours ago      Up 17 seconds (health: starting)   8080/tcp                                    harbor-portal
[root@VM-8-7-centos harbor]# 

5、登录管理后台

#登陆URL
http://43.143.249.82:80/harbor/sign-in

报错:账号密码不正确

[root@VM-8-7-centos harbor]# docker login 43.143.249.82
Username: admin
Password: 
Error response from daemon: login attempt to http://43.143.249.82/v2/ failed with status: 502 Bad Gateway

错误:#postgresql和redis启动不正常如下所示:

#postgresql和redis启动不正常
[root@VM-8-7-centos harbor]# docker-compose ps
WARN[0000] /usr/local/harbor/docker-compose.yml: `version` is obsolete 
NAME                IMAGE                                  COMMAND                   SERVICE       CREATED          STATUS                             PORTS
harbor-core         goharbor/harbor-core:v1.10.18          "/harbor/harbor_core"     core          14 minutes ago   Up 10 seconds (health: starting)   
harbor-db           goharbor/harbor-db:v1.10.18            "/docker-entrypoint.…"   postgresql    14 minutes ago   Restarting (1) 28 seconds ago      
harbor-jobservice   goharbor/harbor-jobservice:v1.10.18    "/harbor/harbor_jobs…"   jobservice    14 minutes ago   Up 13 seconds (health: starting)   
harbor-log          goharbor/harbor-log:v1.10.18           "/bin/sh -c /usr/loc…"   log           14 minutes ago   Up 14 minutes (healthy)            127.0.0.1:1514->10514/tcp
harbor-portal       goharbor/harbor-portal:v1.10.18        "nginx -g 'daemon of…"   portal        14 minutes ago   Up 14 minutes (healthy)            8080/tcp
nginx               goharbor/nginx-photon:v1.10.18         "nginx -g 'daemon of…"   proxy         14 minutes ago   Up 14 minutes (healthy)            0.0.0.0:8089->8080/tcp, :::8089->8080/tcp
redis               goharbor/redis-photon:v1.10.18         "redis-server /etc/r…"   redis         14 minutes ago   Restarting (1) 28 seconds ago      
registry            goharbor/registry-photon:v1.10.18      "/home/harbor/entryp…"   registry      14 minutes ago   Up 14 minutes (healthy)            5000/tcp
registryctl         goharbor/harbor-registryctl:v1.10.18   "/home/harbor/start.…"   registryctl   14 minutes ago   Up 14 minutes (healthy)

解决方式:

查看数据库的挂载目录,执行 cat docker-compose.yml , 看到postgresql数据挂载目录是/data/database。redis数据挂载目录是/data/redis。然后分别进入, 如果没有请创建,如果有了,请删除掉里面的东西(学习可以直接删,企业中上不能删除)


[root@VM-8-7-centos redis]# cd /data/database
[root@VM-8-7-centos database]# ll
总用量 4
drwx------ 19 polkitd input 4096 7月  19 11:38 pg14
[root@VM-8-7-centos database]# rm -rf *
[root@VM-8-7-centos database]# ll
总用量 0
root@VM-8-7-centos database]# cd /data/redis
[root@VM-8-7-centos redis]# ll
总用量 4
-rw-r--r-- 1 polkitd input 88 7月  19 11:38 dump.rdb
[root@VM-8-7-centos redis]# 
[root@VM-8-7-centos redis]# rm -rf *

 然后重启harbor 就正常了如下所示:

[root@VM-8-7-centos harbor]# docker-compose down
WARN[0000] /usr/local/harbor/docker-compose.yml: `version` is obsolete 
[+] Running 10/10
 ✔ Container nginx              Removed                                                                                                             0.2s 
 ✔ Container registryctl        Removed                                                                                                            10.2s 
 ✔ Container harbor-jobservice  Removed                                                                                                             0.2s 
 ✔ Container harbor-portal      Removed                                                                                                             0.3s 
 ✔ Container harbor-core        Removed                                                                                                             0.1s 
 ✔ Container harbor-db          Removed                                                                                                             0.3s 
 ✔ Container registry           Removed                                                                                                            10.2s 
 ✔ Container redis              Removed                                                                                                             0.0s 
 ✔ Container harbor-log         Removed                                                                                                            10.2s 
 ✔ Network harbor_harbor        Removed                                                                                                             0.1s 
[root@VM-8-7-centos harbor]# 
[root@VM-8-7-centos harbor]# docker-compose up -d
WARN[0000] /usr/local/harbor/docker-compose.yml: `version` is obsolete 
[+] Running 10/10
 ✔ Network harbor_harbor        Created                                                                                                             0.1s 
 ✔ Container harbor-log         Started                                                                                                             0.7s 
 ✔ Container harbor-db          Started                                                                                                             1.2s 
 ✔ Container harbor-portal      Started                                                                                                             1.3s 
 ✔ Container redis              Started                                                                                                             1.4s 
 ✔ Container registry           Started                                                                                                             1.4s 
 ✔ Container registryctl        Started                                                                                                             1.3s 
 ✔ Container harbor-core        Started                                                                                                             1.6s 
 ✔ Container nginx              Started                                                                                                             2.0s 
 ✔ Container harbor-jobservice  Started                                                                                                             2.0s 
[root@VM-8-7-centos harbor]# docker-compose ps
WARN[0000] /usr/local/harbor/docker-compose.yml: `version` is obsolete 
NAME                IMAGE                                  COMMAND                   SERVICE       CREATED         STATUS                            PORTS
harbor-core         goharbor/harbor-core:v1.10.18          "/harbor/harbor_core"     core          9 seconds ago   Up 7 seconds (health: starting)   
harbor-db           goharbor/harbor-db:v1.10.18            "/docker-entrypoint.…"   postgresql    9 seconds ago   Up 7 seconds (health: starting)   5432/tcp
harbor-jobservice   goharbor/harbor-jobservice:v1.10.18    "/harbor/harbor_jobs…"   jobservice    9 seconds ago   Up 6 seconds (health: starting)   
harbor-log          goharbor/harbor-log:v1.10.18           "/bin/sh -c /usr/loc…"   log           9 seconds ago   Up 8 seconds (health: starting)   127.0.0.1:1514->10514/tcp
harbor-portal       goharbor/harbor-portal:v1.10.18        "nginx -g 'daemon of…"   portal        9 seconds ago   Up 7 seconds (health: starting)   8080/tcp
nginx               goharbor/nginx-photon:v1.10.18         "nginx -g 'daemon of…"   proxy         9 seconds ago   Up 6 seconds (health: starting)   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp
redis               goharbor/redis-photon:v1.10.18         "redis-server /etc/r…"   redis         9 seconds ago   Up 7 seconds (health: starting)   6379/tcp
registry            goharbor/registry-photon:v1.10.18      "/home/harbor/entryp…"   registry      9 seconds ago   Up 7 seconds (health: starting)   5000/tcp
registryctl         goharbor/harbor-registryctl:v1.10.18   "/home/harbor/start.…"   registryctl   9 seconds ago   Up 7 seconds (health: starting)   
[root@VM-8-7-centos harbor]# 

 

三、使用Harbor

网上一搜一大片........


原文地址:https://blog.csdn.net/LIU_ZHAO_YANG/article/details/140525716

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!