Docker 镜像仓库(Registry)服务本地化

关于【Registry】

  • 官方的【Docker hub】是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去。但是,有时候,我们的使用场景需要我们拥有一个私有的镜像仓库用于管理我们自己的镜像。这个可以通过开源软件Registry来达成目的。
  • 【Registry】在github上有两份代码:老代码库和新代码库。老代码是采用python编写的,存在pull和push的性能问题,出到0.9.1版本之后就标志为deprecated(查看更多信息=》https://github.com/docker/distribution),不再继续开发。
  • 从2.0版本开始就到在新代码库进行开发,新代码库是采用go语言编写,修改了镜像id的生成算法、registry上镜像的保存结构,大大优化了pull和push镜像的效率。【Registry2.0】=》https://hub.docker.com/_/registry/

【Regisry可以做什么?】

  1. 严格的控制镜像的存储位置(tightly control where your images are being stored);
  2. 完全拥有镜像分发管道(fully own your images distribution pipeline);
  3. 将镜像存储和分发紧密集成到内部开发工作流中(integrate image storage and distribution tightly into your in-house development workflow);

注:关于【Registry】的官网介绍=》https://docs.docker.com/registry/

如何部署 Registry ?

  • 从【Docker Hub】下载 registry 镜像=》docker pull registry 下载的默认版本为 docker.io/registry latest
  • 运行 registry 镜像生成一个容器 =》docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest 【Registry】服务默认会将上传的镜像保存在容器的/var/lib/registry,将主机的/opt/registry目录挂载到该目录,即可实现将镜像保存到主机的/opt/registry目录了。
  • 查看运行的容器 =》docker ps / docker container ls 若查看所有,添加参数 -a
  • 若 registry 容器启动运行,在浏览器输入【http://docker主机ip:5000/v2/】查看,若输出 =》{} 说明 registry 运行正常

【测试 registry】

  • 上传镜像到registry,首先将主机的 registry 镜像命名为符合仓库要求的格式 =》【registry_url:port/ImageName:tag】
--1.先标记镜像格式
docker tag nginx:latest localhost:5000/nginx:latest

--2.把标记格式的镜像推送到registy
docker push localhost:5000/nginx:latest

--注:localhost 为本地环境的ip地址;
  • 查看本地所有镜像列表 
docker images -a 或 docker image ls -a
  • 从本地 registry 仓库拉取镜像

docker pull localhost:5000/nginx:latest

--注:localhost 为本地环境的ip地址;

这一步可能出现无法推送镜像(push)到私有仓库的问题,提示:

The push refers to a repository [localhost:5000/nginx:latest]
The https://localhsot:5000/v2/: http:server gave HTTP response to HTTPS client

 1、原因是本地启动的 registry 服务不是安全可信任的;

 2、解决方案,修改客户端 docker 的配置文件 /etc/docker/daemon.json 添加 registry 服务地址

{
"registry-mirrors": [ "https://pee6w651.mirror.aliyuncs.com"],"insecure-registries": ["localhost:5000"]
}

--备注:
1.加速镜像设置 =》"registry-mirrors": ["https://pee6w651.mirror.aliyuncs.com"] 是阿里云代理的Registry Hub仓库的地址,可以加快国内访问【Registry Hub】仓库的速度;
2.私有仓库镜像设置 =》"insecure-registries": ["localhost:5000"] 是本地【Registry】服务地址,localhost 为当前 docker 主机ip;

 3、【daemon.json】 存放路径=》 cd /etc/docker

 

 4、查看/编辑 daemon.json 文件配置信息 =》 vi daemon.json

 5、退出并保存 =》:wq

修改好后重启 Docker 服务 daemon.json 配置才生效,执行命令 =》systemctl restart docker , 再次push即可成功,查看本地 /opt/registry 目录下面(或者浏览器输入 http://localhost:5000/v2/_catalog)

  • 或者使用以下命令查看本地私有仓库里面的镜像信息
  1. curl -XGET http://registry:5000/v2/_catalog
  2. curl -XGET http://registry:5000/v2/image_name/tags/list

【附:镜像加速地址】

  • 阿里云镜像加速器=》https://help.aliyun.com/document_detail/60750.html
  • 网易云镜像加速器=》https://hub-mirror.c.163.com
  • 官方中国镜像加速器=》https://regisry.docker-cn.com
  • 中科大镜像加速器=》https://docker.mirrors.ustc.edu.cn
  • 清华镜像加速器=》https://mirrors.tuna.tsinghua.edu.cn

以上步骤就实现了本地部署 Registry 的过程,感兴趣的小伙伴,赶快动手跟着小c老师一步一步的实现吧! 

猜你喜欢

转载自blog.csdn.net/ChaITSimpleLove/article/details/105281751