搭建Docker Registry

docker registry 用于存储 docker 镜像,是用于存储仓库,Docker Hub 是 docker 官方提供的一个公有仓库,这也是Docker默认的镜像仓库。

docker 允许修改镜像仓库,支持修改为非官方的共有仓库或不同形式的私有仓库。

本文介绍了搭建私有仓库(docker registry)的方法。

安装

  1. 拉取镜像
docker pull registry
  1. 启动registry服务
docker run -d -p 5000:5000 --name registry registry

# 添加数据卷
# 其中 /home/xzbd/appData/docker/registry 是本机地址
docker run -d -p 5000:5000 -v /home/xzbd/appData/docker/registry:/var/lib/registry --name registry registry 

  1. 测试registry服务

registry 服务启动成功后,可以通过rest api 检测是否成功。

一般访问镜像仓库V2版本检测接口即可,接口:localhost:5000/v2

可以使用浏览器访问,结果如下:

在这里插入图片描述

也可以通过http客户端工具访问,访问结果如下:

PS D:\Users\zxbd> curl http://localhost:5000/v2


StatusCode        : 200
StatusDescription : OK
Content           : {}
RawContent        : HTTP/1.1 200 OK
                    Docker-Distribution-Api-Version: registry/2.0
                    X-Content-Type-Options: nosniff
                    Content-Length: 2
                    Content-Type: application/json; charset=utf-8
                    Date: Sat, 13 Aug 2022 07:06:26 GMT
                    ...
Forms             : {}
Headers           : {[Docker-Distribution-Api-Version, registry/2.0], [X-Content-Type-Options, nosniff], [Content-Lengt
                    h, 2], [Content-Type, application/json; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 2
  1. 将公有仓库的镜像推送到私有仓库

要将公有仓库的镜像推送到私有仓库,需要三步操作,以推送 busybox 为例,步骤如下:

a. 拉取公有镜像

docker pull busybox

拉取完成后可使用 docker images 查看本地镜像,从中可以查到 busybox:latest

b. 将公有镜像打包为私有仓库镜像

按照规范,要将镜像包推送到私有镜像仓库,镜像包需要按照标准命名规范打包镜像才行。

镜像包标准命名规范为 协议://ip:port/imageName:tag,其中协议://ip:port 可以使用DNS域名代替。
协议一般分 http和https,具体示例如下:

http 模式https 模式
http://localhost:port/imageName:taghttps://localhost:port/imageName:tag
localhost:port/imageName:taghttps://localhost:port/imageName:tag
http://ip:port/imageName:taghttps://ip:port/imageName:tag
ip:port/imageName:taghttps://ip:port/imageName:tag
dns域名/imageName:tagdns域名:port/imageName:tag

简单的看,镜像命名是由"/"分开的两部分组成,格式为:仓库地址/镜像版本,仓库地址不存在时表示docker默认仓库地址,如 Docker Hub 等

其中:ip 为镜像服务ip地址;port 为 镜像服务端口;imageName 为镜像名称;tag 为镜像版本,tag可以为空,不写时其值为 latest;

将 busybox 镜像打包为本地仓库镜像包

docker image tag busybox localhost:5000/busbox:v1

打包完成后可以使用 docker images 查看打包情况。

c. 上传镜像到私有仓库

使用 docker push 推送镜像时,docker 会按照镜像名称格式仓库地址/镜像版本解析信息,将名称描述的版本镜像推送到名称描述的仓库中,如果名称没有描述仓库信息,则推送到 docker 默认镜像仓库中,如果镜像版本没有表述,则推送该镜像的 latest 版本到描述的镜像中。

docker push localhost:5000/busbox:v1

推送完成后可以使用 registry 提供的API查看仓库中的镜像信息。

http://localhost:5000/v2/_catalog

用浏览器访问返回结果为:

{"repositories":["busbox"]}

在这里插入图片描述
curl 访问接口如下:

PS D:\Users\xzbd> curl http://localhost:5000/v2/_catalog


StatusCode        : 200
StatusDescription : OK
Content           : {"repositories":["busbox"]}

RawContent        : HTTP/1.1 200 OK
                    Docker-Distribution-Api-Version: registry/2.0
                    X-Content-Type-Options: nosniff
                    Content-Length: 28
                    Content-Type: application/json; charset=utf-8
                    Date: Sat, 13 Aug 2022 08:17:22 GMT...
Forms             : {}
Headers           : {[Docker-Distribution-Api-Version, registry/2.0], [X-Content-Type-Options, nosniff], [Content-Lengt
                    h, 28], [Content-Type, application/json; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 28
  1. 从私有仓库拉取镜像

从私有仓库拉取镜像需要在输入镜像信息是描述镜像地址信息,如果不描述,docker 会从默认仓库中拉取信息。

docker pull localhost:5000/busbox:v1

总结

相关连接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值