但有时候使用Docker Hub这样的公共仓库可能不方便,这种情况下用户可以使用registry创建一个本地仓库供私人使用,这点跟Maven的管理类似。
使用私有仓库有许多优点:
节省网络带宽,针对于每个镜像不用每个人都去中央仓库上面去下载,只需要从私有仓库中下载即可;
提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,以供公司内部相关人员使用。
目前Docker Registry已经升级到了v2,最新版的Docker已不再支持v1。Registry v2使用Go语言编写,在性能和安全性上做了很多优化,重新设计了镜像的存储格式。如果需要安装registry v2,只需下载registry:2.2即可。Docker官方提供的工具docker-registry可以用于构建私有的镜像仓库。废话不多说了,下面记录下Docker私有仓库构建的过程:
选择一台服务器(内外网地址:192.168.31.10)作为注册服务器,用于搭建私有仓库。(该机器要安装了Docker环境)
1)从Docker官方仓库里下载registry镜像
[root@lcalhost ~]# docker pull registry:2.2
2.2: Pulling from library/registry
8387d9ff0016: Pull complete
3b52deaaf0ed: Pull complete
4bd501fad6de: Pull complete
a3ed95caeb02: Pull complete
b1f99b5938be: Pull complete
82c34c0ec017: Pull complete
5426c0c1c293: Pull complete
Digest: sha256:a9128f456da044c4132c41973b065d2e2c46b04682c65f23dbb5d0c51071d595
Status: Downloaded newer image for registry:2.2
下载完之后,可以通过该镜像启动一个容器
[root@lcalhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine <none> f70734b6a266 3 days ago 5.61MB
centos latest 470671670cac 3 months ago 237MB
ubuntu 14.04 6e4f1fe62ff1 4 months ago 197MB
registry 2.2 ad379b517aa6 4 years ago 225MB
默认情况下,会将私有仓库存放于容器内的/tmp/registry目录下,这样如果容器被删除,则存放于容器中的镜像也会丢失。
所以一般情况下会指定本地一个目录挂载到容器内的/tmp/registry下,如下:
[root@lcalhost ~]# docker run -d --name=my_registry -p 5000:5000 -v /opt/data/registry:/tmp/registry registry:2.2
97e2121565b8d5166ddf9fe98feadfb2ae2402ab608b7804c40a50df7d5139e6
[root@lcalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97e2121565b8 registry:2.2 "/bin/registry /etc/…" 6 seconds ago Up 4 seconds 0.0.0.0:5000->5000/tcp my_registry
由上可以看到,已经启动了一个容器,地址为:192.168.31.10:5000。
2)测试
接下来可以把一个本地镜像push(如下面的tomcat7镜像)到私有仓库中。
[root@lcalhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine <none> f70734b6a266 3 days ago 5.61MB
centos latest 470671670cac 3 months ago 237MB
ubuntu 14.04 6e4f1fe62ff1 4 months ago 197MB
registry 2.2 ad379b517aa6 4 years ago 225MB
修改一下该镜像的tag标识。
[root@lcalhost ~]# docker tag centos 192.168.31.10:5000/centos //修改了tag后的镜像若要删除,docker rmi后面不能用镜像ID了,需要用docker rmi 192.168.1.23:5000/tomcat7:latest
[root@lcalhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine <none> f70734b6a266 3 days ago 5.61MB
192.168.31.10:5000/centos latest 470671670cac 3 months ago 237MB
centos latest 470671670cac 3 months ago 237MB
ubuntu 14.04 6e4f1fe62ff1 4 months ago 197MB
registry 2.2 ad379b517aa6 4 years ago 225MB
接下来把上面修改tag后的镜像上传到私有仓库。
[root@lcalhost ~]# docker push 192.168.31.10:5000/centos:latest
The push refers to repository [192.168.31.10:5000/centos]
Get https://192.168.31.10:5000/v2/: http: server gave HTTP response to HTTPS client
出现上面错误的原因分析:
因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。
为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。
需要在docker的配置文件/etc/sysconfig/docker 添加参数“--insecure-registry=192.168.31.10:5000”。
-----------------------------------------------------------------------------------------------------------
温馨提示:
这个是在客户机的docker配置文件里添加的(即上传镜像到私有仓库里或从私有仓库下载镜像的客户机)。
比如说在A机器上将它的镜像上传到192.168.31.10的私有仓库上或从该私有仓库下载镜像,那么就在A机器的本地docker配置文件中添加。
我这里测试用的是同一台机器(centos7),即将注册机192.168.31.10本机的镜像上传到它的仓库内。
-----------------------------------------------------------------------------------------------------------
[root@localhost ~]# vim /etc/sysconfig/docker
.......
OPTIONS='--selinux-enabled --log-driver=journald'
改为
OPTIONS='--selinux-enabled --log-driver=journald --insecure-registry=192.168.1.23:5000'
[root@lcalhost ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
由于docker服务重启后,所有容器都会被关闭。所以需要在docker重启后再次启动容器
[root@lcalhost ~]# docker start my_registry
my_registry
[root@lcalhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90583247428d alpine:latest "ping docker.com" About a minute ago Up 54 seconds myhelloworld.1.qlsmeqzaetsjkxzf1rarn95tq
e6ddff95dd72 ubuntu:14.04 "ping docker.com" About a minute ago Up 54 seconds mytest.1.my11iws82l2c7gh38i7zcfnei
97e2121565b8 registry:2.2 "/bin/registry /etc/…" 11 minutes ago Up 5 seconds 0.0.0.0:5000->5000/tcp my_registry
再次提交到私有仓库
[root@localhost ~]# docker push 192.168.31.10:5000/tomcat7
The push refers to a repository [192.168.31.10:5000/tomcat7]
c6d7ce9e90d7: Pushed
34e7b85d83e4: Pushed
latest: digest: sha256:5fdcbaf254cb44dd26645f606cccea8de76118baff03485e40853c691a15956d size: 720
上面命令执行无误后,就表示镜像已经push到私有仓库中去了。
查看私有仓库里的镜像(一定要保证下面能查看到仓库里有镜像!如果仓库里没有镜像,那么客户端机器就无法从该私有仓库下载镜像了)
[root@localhost ~]# curl -XGET http://192.168.31.10:5000/v2/_catalog //即该私有仓库里有centos镜像
{"repositories":["centos"]}
[root@localhost ~]# curl -XGET http://192.168.31.10:5000/v2/centos/tags/list
{"name":"centos","tags":["latest"]}
或者浏览器里访问(103.110.186.23是注册机的外网ip,iptables防火墙内开放5000端口访问):
http://103.110.186.23:5000/v2/_catalog
http://103.110.186.23:5000/v2/tomcat7/tags/list
现在可以将本地的centos和192.168.31.10:5000/centos镜像都删除,然后从私有仓库中下载
[root@localhost ~]# docker rmi centos:latest
[root@localhost ~]# docker rmi 192.168.31.10:5000/tomcat7
[root@localhost ~]# docker pull 192.168.31.10:5000/centos
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.31.10:5000/centos latest 5hc8a2ip413w 3 days ago 562.3 MB
-----------------------------------------------------------------------------
这样,也就可以在同一局域网内的其他机器上,从该私有仓库中pull下来该镜像。
比如在192.168.31.17服务器上拉取该私有仓库的centos镜像进行容器创建(注意,要在该机器的/etc/sysconfig/docker配置文件里添加--insecure-registry=192.168.31.10:5000参数)
[root@linux-node2 ~]# docker pull 192.168.31.10:5000/centos
[root@linux-node2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.31.10:5000/centos latest 2ec9e2eb978a 3 days ago 562.3 MB
这样就搭建了Docker私有仓库,上面搭建的仓库是不需要认证的,我们可以结合nginx和https实现认证和加密功能。
注意查看镜像方法(docker pull registry:2.1.1):
# curl -XGET http://registry_ip:5000/v2/_catalog
# curl -XGET http://registry_ip:5000/v2/image_name/tags/list