Docker搭建官方私有仓库registry及相关配置

(企业不推荐使用registry) 在 Docker 中,当我们执行 docker pull xxx 的时候 ,它实际上是从 https://registry.hub.docker.com/ 这个地址去查找,这就是Docker公司为我们提供的公共仓库。

在工作中,我们不可能把企业项目push到公有仓库进行管理。所以为了更好的管理镜像,Docker不仅提供了一个中央仓库,同时也允许我们搭建本地私有仓库。
docker 官方提供的私有仓库 registry,用起来虽然简单 ,但在管理的功能上存在不足。 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,harbor使用的是官方的docker registry(v2命名是distribution)服务去完成。harbor在docker distribution的基础上增加了一些安全、访问控制、管理的功能以满足企业对于镜像仓库的需求。

下载地址:https://github.com/goharbor/harbor/releases
此文介绍registry私有仓库搭建方法。


1、私有仓库搭建与配置

(0)搜索registry镜像(可跳过)

bash 复制代码
[root@localhost java]# docker search registry

  
  
  

(1)拉取最新版(latest)私有仓库镜像:registry

bash 复制代码
[root@localhost java]# docker pull registry

  
  
  

查看镜像:


(2)创建并启动私有仓库容器(默认已启动)

bash 复制代码
[root@localhost java]# docker run -di --name=registry -p 5000:5000 registry:latest

  
  
  

查看已启动的容器:


(3)暂时关闭防火墙(根据实际情况可跳过

bash 复制代码
[root@localhost ~]# systemctl stop firewalld

  
  
  

重启防火墙命令:service iptables restart


(4)打开浏览器,输入地址:http://192.168.116.161:5000/v2/_catalog 看到{"repositories":[]} 表示私有仓库搭建成功并且内容为空。

注:192.168.116.161 为linux服务器宿主系统IP。


(5)修改daemon.json(让docker信任私有仓库地址

bash 复制代码
[root@localhost java]# vi /etc/docker/daemon.json

  
  
  

添加以下内容,保存退出。

json 复制代码
{"insecure-registries":["192.168.116.161:5000"]} 

  
  
  

内容如下图:


(6)重启docker 服务

bash 复制代码
[root@localhost java]# systemctl daemon-reload
[root@localhost java]# systemctl restart docker

  
  
  

2、镜像上传至私有仓库

(1)标记镜像为私有仓库的镜像

使用 docker tag 命令标记本地镜像 jdk1.8,将其归入某一仓库。

格式:

docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

【示例】:

原有镜像:

如,将镜像 jdk1.8:latest 标记为 192.168.116.161:5000/jdk1.8 镜像:

bash 复制代码
[root@localhost java]# docker tag jdk1.8:latest 192.168.116.161:5000/jdk1.8

  
  
  

(2)启动私服容器(registry)

bash 复制代码
[root@localhost java]# docker start registry

  
  
  

(3)上传标记的镜像

bash 复制代码
[root@localhost java]# docker push 192.168.116.161:5000/jdk1.8

  
  
  

(4)访问私服仓库

注:192.168.116.161 为linux服务器宿主系统IP。

第1种:命令行输出

bash 复制代码
[root@localhost java]# curl http://192.168.116.161:5000/v2/_catalog
{"repositories":["jdk1.8"]}
[root@localhost java]# 

  
  
  

第2种:浏览器输出

浏览器访问私服:http://192.168.116.161:5000/v2/_catalog

如下图,可以看到jdk1.8已经上传到了私有仓库中:


2、附:删除私有仓库镜像方法

(1)获取私有仓库镜像sha256值

格式:

curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://镜像IP地址/v2/镜像名称/manifests/镜像版本

如,获取springboot_demo的sha256值的命令:

curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://`192.168.116.161`:5000/v2/`springboot_demo`/manifests/`0.0.1-SNAPSHOT`

操作详情:

bash 复制代码
[root@localhost java]# curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://192.168.116.161:5000/v2/springboot_demo/manifests/0.0.1-SNAPSHOT
HTTP/1.1 200 OK
Content-Length: 949
Content-Type: application/vnd.docker.distribution.manifest.v2+json
Docker-Content-Digest: sha256:8b4595870df7c01953970ea497da6a8291f90a7cef08cc42580aa4b4f914dee1
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:8b4595870df7c01953970ea497da6a8291f90a7cef08cc42580aa4b4f914dee1"
X-Content-Type-Options: nosniff
Date: Mon, 30 Nov 2020 19:01:34 GMT
[root@localhost java]#

  
  
  

(2)删除私有仓库镜像

格式:

curl -I -X DELETE http://镜像IP地址:5000/v2/镜像名称/manifests/镜像对应sha256值

如,删除私有仓库springboot_demo镜像的命令:

curl -I -X DELETE http://`192.168.116.161`:5000/v2/`springboot_demo`/manifests/`8b4595870df7c01953970ea497da6a8291f90a7cef08cc42580aa4b4f914dee1`

操作详情:

bash 复制代码
[root@localhost java]# curl -I -X DELETE http://192.168.116.161:5000/v2/springboot_demo/manifests/8b4595870df7c01953970ea497da6a8291f90a7cef08cc42580aa4b4f914dee1
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json; charset=utf-8
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Mon, 30 Nov 2020 19:06:38 GMT
Content-Length: 78
[root@localhost java]#

  
  
  

如上,提示405没有权限。权限问题,暂时不知道如何解决。

相关推荐
明月心95236 分钟前
NFS CENTOS系统 安装配置
linux·运维·centos
Raymond运维42 分钟前
MySQL 8.0源码编译安装(二)
linux·运维·mysql
延凡科技1 小时前
延凡科技光伏清洗预测系统
大数据·运维·人工智能·科技·无人机·智慧城市
gogogo出发啦2 小时前
JVM快速入门
linux·运维·jvm
core5122 小时前
fastdfs快速部署、集成、调优
docker·部署·springboot·fastdfs·调用
wayuncn2 小时前
16核32G硬件服务器租用需要多少钱
运维·服务器·服务器租用·裸金属服务器·物理服务器租用·物理机租用·idc服务商
木雷坞2 小时前
docker国内镜像源列表
运维·docker·容器
Hoxy.R3 小时前
一次“无告警”的服务器宕机分析:从无迹可寻到精准定位
运维·服务器
小孙姐3 小时前
Linux-Day11.WEB服务,虚拟主机
linux·运维·服务器
iLoyalty3 小时前
防御保护07-08
运维·服务器