# Docker私有仓库搭建与使用

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/20699.html | 发布：2023-05-23 | 更新：2023-05-23 | 分类：建站教程

## 1. Docker Registry

 

网上有很多的Registry服务器都支持第三方用户注册，而后基于用户名去做自己的仓库，但是使用互联网上的Registry有一个缺陷，那就是我们去推送和下载镜像时都不会很快，而在生产环境中很可能并行启动的容器将达到几十、上百个，而且很有可能每个服务器本地是没有镜像的，此时如果通过互联网去下载镜像会有很多问题，比如下载速度会很慢、带宽会用很多等等，如果带宽不够的话，下载至启动这个过程可能要持续个几十分钟，这已然违背了使用容器会更加轻量、快速的初衷和目的。因此，很多时候我们很有可能需要去做自己的私有Registry。  
**Docker Registry分类：**

 

**Sponsor Registry：第三方的Registry，供客户和Docker社区使用  
Mirror Registry：第三方的Registry，只让客户使用  
Vendor Registry：由发布docker镜像的供应商提供的registry  
Private Registry：通过设有防火墙和额外的安全层的私有实体提供的registry**

 

事实上，如果运维的系统环境托管在云计算服务上，比如阿里云，那么用阿里云的Registry则是最好的选择。很多时候我们的生产环境不会在本地，而是托管在数据中心机房里，如果我们在数据中心机房里的某台主机上部署Registry，因为都在同一机房，所以属于同一局域网，此时数据传输走内网，效率会极大的提升。

 

所有的Registry默认情况下都是基于https工作的，这是Docker的基本要求，而我自建Registry时很可能是基于http工作的，但是Docker默认是拒绝使用http提供Registry服务的，除非明确的告诉它，我们就是要用http协议的Registry。

 

## 2. Docker私有Registry

 

为了帮助我们快速创建私有Registry，Docker专门提供了一个名为docker-distribution的软件包，我们可以通过安装这个软件包快速构建私有仓库。

 

当然，官方也提供了Registry的镜像，我们可以直接将其pull到本地并启动为容器即可快速实现私有Registry。

 

在node01上使用自建的Registry去上传镜像

 

//使用insecure-registries参数添加http支持  
[root@node01 ~]# vim /etc/docker/daemon.json  
{  
 "registry-mirrors": ["https://registry.docker-cn.com"],  
 "insecure-registries": ["172.16.78.128:5000"]  
}  
[root@node01 ~]# systemctl restart docker

 

//将本地镜像传到自建的registry中  
[root@node01 ~]# docker images   
REPOSITORY TAG IMAGE ID CREATED SIZE  
docker.io/httpd latest a6ea92c35c43 3 weeks ago 166 MB  
[root@node01 ~]# docker tag httpd:latest node02:5000/httpd:latest  
[root@node01 ~]# docker images  
REPOSITORY TAG IMAGE ID CREATED SIZE  
172.16.78.128:5000/httpd latest a6ea92c35c43 3 weeks ago 166 MB  
docker.io/httpd latest a6ea92c35c43 3 weeks ago 166 MB  
[root@node01 ~]# docker push 172.16.78.128:5000/httpd  
The push refers to a repository [172.16.78.128:5000/httpd]  
88b680b1fdfc: Pushed   
843c3701e622: Pushed   
3ba8a4f66ba2: Pushed   
c865989f86f7: Pushed   
d0f104dc0a1f: Pushed   
latest: digest: sha256:fc717ed0d0b55ada05af1c1a95a2d4ee1153a5858fd65b654644a1a5add0c28b size: 1367

 

在node01上使用自建的Registry去上传镜像

 

//使用insecure-registries参数添加http支持  
[root@node01 ~]# vim /etc/docker/daemon.json  
{  
 "registry-mirrors": ["https://registry.docker-cn.com"],  
 "insecure-registries": ["172.16.78.128:5000"]  
}  
[root@node01 ~]# systemctl restart docker

 

//将本地镜像传到自建的registry中  
[root@node01 ~]# docker images   
REPOSITORY TAG IMAGE ID CREATED SIZE  
docker.io/httpd latest a6ea92c35c43 3 weeks ago 166 MB  
[root@node01 ~]# docker tag httpd:latest node02:5000/httpd:latest  
[root@node01 ~]# docker images  
REPOSITORY TAG IMAGE ID CREATED SIZE  
172.16.78.128:5000/httpd latest a6ea92c35c43 3 weeks ago 166 MB  
docker.io/httpd latest a6ea92c35c43 3 weeks ago 166 MB  
[root@node01 ~]# docker push 172.16.78.128:5000/httpd  
The push refers to a repository [172.16.78.128:5000/httpd]  
88b680b1fdfc: Pushed   
843c3701e622: Pushed   
3ba8a4f66ba2: Pushed   
c865989f86f7: Pushed   
d0f104dc0a1f: Pushed   
latest: digest: sha256:fc717ed0d0b55ada05af1c1a95a2d4ee1153a5858fd65b654644a1a5add0c28b size: 1367

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
