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镜像(可跳过)

[root@localhost java]# docker search registry

在这里插入图片描述


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

[root@localhost java]# docker pull registry

在这里插入图片描述
查看镜像:
在这里插入图片描述


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

[root@localhost java]# docker run -di --name=registry -p 5000:5000 registry:latest

查看已启动的容器:
在这里插入图片描述


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

[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信任私有仓库地址

[root@localhost java]# vi /etc/docker/daemon.json

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

{"insecure-registries":["192.168.116.161:5000"]} 

内容如下图:
在这里插入图片描述


(6)重启docker 服务

[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 镜像:

[root@localhost java]# docker tag jdk1.8:latest 192.168.116.161:5000/jdk1.8

在这里插入图片描述


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

[root@localhost java]# docker start registry

(3)上传标记的镜像

[root@localhost java]# docker push 192.168.116.161:5000/jdk1.8

在这里插入图片描述


(4)访问私服仓库

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

第1种:命令行输出

[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

操作详情:

[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

操作详情:

[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没有权限。权限问题,暂时不知道如何解决。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值