docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --name myregistry registry:2
>
> registry服务默认将上传的镜像保存在容器的`/var/lib/registry`,使用`-v`参数将容器的/var/lib/registry目录映射到本地/opt/registry目录。即可实现将镜像保存到宿主机/opt/registry目录。
>
>
>
浏览器访问`http://宿主机IP:5000/v2`,显示“`{}`” 说明registry运行正常。
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X3BuZy9jYkNMZ2ZKWmlicHJpY250bVdXZXF6WWYwUkhNY0kwNEVlOTZvU29LUXBZNTA2WEVlTDViNnBPeXMzTkpNNXkzQzNVRE0yaHJOTlVqWVo3RWlhNk1nNVNWZy82NDA?x-oss-process=image/format,png)
### 上传镜像到私有仓库
现在通过push将镜像上传至私有仓库,具体步骤如下:
1、下载docker hub官方镜像
docker pull ubuntu
2、将镜像标志为要推送到私有仓库:
docker tag ubuntu:latest 10.43.187.251:5000/myubuntu:v1
3、上传镜像到私有仓库
[root@qll251 ~]# docker push 10.43.187.251:5000/myubuntu:v1
The push refers to repository [10.43.187.251:5000/myubuntu]
Get https://10.43.187.251:5000/v2/: http: server gave HTTP response to HTTPS client
注意,上传镜像时报错了:`http: server gave HTTP response to HTTPS client`
>
> 出现这个问题原因是:**Docker自从1.3.X之后docker registry交互默认使用的是HTTPS,但是搭建私有镜像默认使用的是HTTP服务,所以与私有仓库交互时出现以上错误。**
>
>
>
解决办法:
1、编辑 `/etc/docker/daemon.json`,在文件中写入:
{ “insecure-registries”:[“10.43.187.251:5000”] }
2、重启生效:
systemctl daemon-reload
systemctl restart docker
再次上传,问题解决:
docker pull 10.43.187.251:5000/myubuntu:v1
### 客户端下载私有镜像
我们在另外一台docker机器上,进行pull测试:
[root@qll252 ~]# docker pull 10.43.187.251:5000/myubuntu:v1
Trying to pull repository 10.43.187.251:5000/myubuntu …
Get https://10.43.187.251:5000/v1/_ping: http: server gave HTTP response to HTTPS client
会发现跟前面上传镜像报了同样的错误。
**解决方法同上:**
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X3BuZy9jYkNMZ2ZKWmlicHJpY250bVdXZXF6WWYwUkhNY0kwNEVlbzRpY2JwRElaRkxwU29FaWFCU05NWFdDWFlWQnJpY1F4TVhtcmpLbVVRallHRFVmWDZBUUJzTGJBLzY0MA?x-oss-process=image/format,png)
再次从私有仓库中下载镜像:
[root@qll252 ~]# docker pull 10.43.187.251:5000/myubuntu:v1
Trying to pull repository 10.43.187.251:5000/myubuntu …
v1: Pulling from 10.43.187.251:5000/myubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8
Status: Downloaded newer image for 10.43.187.251:5000/myubuntu:v1
验证通过,到此已完成私有仓库的搭建工作了。
>
> 说明:在后续教程我们学习到`docker-compose`时,再为大家介绍企业级Harbor私有仓库及搭建过程。
>
>
>