1)节省网络带宽,针对于每个镜像不用每个人都去仓库上面去下载,只需要从私有仓库中下载即可; 2)提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,以供公司内部相关人员使用。 目前Docker Registry已经升级到了v2,最新版的Docker已不再支持v1。Registry v2使用Go语言编写,在性能和安全性上做了很多优化,重新设计了镜像的存储格式。如果需要安装registry v2,只需下载registry:2.2即可。Docker官方提供的工具docker-registry可以用于构建私有的镜像仓库。废话不多说了,下面记录下Docker私有仓库构建的过程: ? 1 选择一台服务器(内地址:192.168.1.23)作为注册服务器,用于搭建私2 有仓库。(该机器要安装了Docker环境) 3 1)从Docker官方仓库里下载registry镜像 4 [root@localhost~]# docker pull registry:2.2 5 ---------------------------------------------------------------- 6 或者: 7 [root@localhost~]# docker pull registry 8 [root@localhost~]# docker pull registry:2.1.1 9 ---------------------------------------------------------------- 1下载完之后,可以通过该镜像启动一个容器 0 [root@localhost~]# docker images 1REPOSITORY TAG 1 IMAGE ID CREATED SIZE 1tomcat7 latest 2 97c6a43dd69c 12 minutes ago 562.3 MB 1docker.io/registry 2.2 ad379b517aa3 6 14 months ago 224.5 MB 1 4 默认情况下,会将私有仓库存放于容器内的/tmp/registry目录下,这样如果1容器被删除,则存放于容器中的镜像也会丢失。 5 所以一般情况下会指定本地一个目录挂载到容器内的/tmp/registry下,如下: 1[root@localhost~]# docker run -d --name=my_registry -p 5000:5000 -v 6 /opt/data/registry:/tmp/registry docker.io/registry:2.2 19fe45329bda17f61da04e6e8d2faf124fb22665a25270421bb79a419809446 7 1[root@localhost~]# docker ps 8 CONTAINER 1ID IMAGE COMMAN9 D CREATED 2 STATUS PORTS 0 NAMES 2d8e98b1068cd docker.io/registry:2.2 \"/bin/registr1 y /etc/d\" About a minute ago Up About a 2minute 0.0.0.0:5000->5000/tcp my_registry 2 2由上可以看到,已经启动了一个容器,地址为:192.168.1.23:5000。 3 22)测试 4 接下来可以把一个本地镜像push(如下面的tomcat7镜像)到私有仓库中。 2[root@localhost~]# docker images 5 REPOSITORY TAG 2IMAGE ID CREATED SIZE 6 tomcat7 latest 297c6a43dd69c 18 minutes ago 562.3 MB 7 docker.io/registry 2.2 ad379b517aa26 14 months ago 224.5 MB 8 2修改一下该镜像的tag标识。 9 [root@localhost~]# docker tag tomcat7 192.168.1.23:5000/tomcat7 3 0 [root@localhost ~]# docker tag tomcat7 3192.168.1.23:5000/tomcat7 //修改了tag后的镜像若要删除,1 docker rmi后面不能用镜像ID了,需要用docker rmi 3192.168.1.23:5000/tomcat7:latest 2 REPOSITORY TAG 3 IMAGE 3 ID CREATED SIZE 3192.168.1.23:5000/tomcat7 latest 97c6a434 dd69c 18 minutes ago 562.3 MB 3tomcat7 latest 5 97c6a43dd69c 18 minutes 3ago 562.3 MB 6 docker.io/registry 2.2 ad379b5137aa6 14 months ago 224.5 MB 7 3接下来把上面修改tag后的镜像上传到私有仓库。 8 [root@localhost~]# docker push 192.168.1.23:5000/tomcat7 3The push refers to a repository [192.168.1.23:5000/tomcat7] 9 unable to pingregistry endpoint https://192.168.1.23:5000/v0/ 4v2 pingattempt failed with error: Get https://192.168.1.23:5000/v2/: 0 http: server gave HTTP response to HTTPS client 4v1 pingattempt failed with error: Get 1 https://192.168.1.23:5000/v1/_ping: http: server gave HTTP response to 4HTTPS client 2 4出现上面错误的原因分析: 3 因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然4而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上4 面的错误。 为了解决这个问题需要在启动docker server时增加启动参数为默认使用http5 访问。 4需要在docker的配置文件/etc/sysconfig/docker(ubuntu系统中的docker6 配置文件时/etc/default/docker)添加参数4“--insecure-registry=192.168.1.23:5000”。 7 -------------------------------------------------------------------4---------------------------------------- 8 温馨提示: 4这个是在客户机的docker配置文件里添加的(即上传镜像到私有仓库里或从私9 有仓库下载镜像的客户机)。 5比如说在A机器上将它的镜像上传到192.168.1.23的私有仓库上或从该私有0 仓库下载镜像,那么就在A机器的本地docker配置文件中添加。 5我这里测试用的是同一台机器,即将注册机192.168.1.23本机的镜像上传到1 它的仓库内。 5-------------------------------------------------------------------2 ---------------------------------------- 5[root@localhost~]# vim /etc/sysconfig/docker 3 ....... 5OPTIONS='--selinux-enabled --log-driver=journald' 4 改为 5OPTIONS='--selinux-enabled --log-driver=journald 5 --insecure-registry=192.168.1.23:5000' 5 6 [root@localhost~]# service docker restart 5 7 由于docker服务重启后,所有容器都会被关闭。所以需要在docker重启后再5次启动容器 8 [root@localhost~]# docker start my_registry 5my_registry 9 [root@localhost~]# docker ps 6CONTAINER 0 ID IMAGE COMMAN6D CREATED 1 STATUS PORTS 6 NAMES 2 d8e98b1068cd docker.io/registry:2.2 \"/bin/registr6y /etc/d\" About a minute ago Up About a 3 minute 0.0.0.0:5000->5000/tcp my_registry 6 4 再次提交到私有仓库 6[root@localhost~]# docker push 192.168.1.23:5000/tomcat7 5 The push refers to a repository [192.168.1.23:5000/tomcat7] 6c6d7ce9e90d7: Pushed 46 34e7b85d83e4: Pushed latest: digest: 7 sha256:5fdcbaf254cb44dd265f606cccea8de76118baff03485e40853c691a156956d size: 720 8 6上面命令执行无误后,就表示镜像已经push到私有仓库中去了。 9 查看私有仓库里的镜像(一定要保证下面能查看到仓库里有镜像!如果仓库里7没有镜像,那么客户端机器就无法从该私有仓库下载镜像了) 0 [root@localhost ~]# curl -XGET 7http://192.168.1.23:5000/v2/_catalog //即该私有仓库里有1 tomcat7镜像 7{\"repositories\":[\"tomcat7\"]} 2 [root@localhost~]# curl -XGET 7http://192.168.1.23:5000/v2/tomcat7/tags/list 3 {\"name\":\"tomcat7\ 7或者浏览器里访问(103.110.186.23是注册机的ip,iptables防火墙内4 开放5000端口访问): 7http://103.110.186.23:5000/v2/_catalog 5 http://103.110.186.23:5000/v2/tomcat7/tags/list 7 6 现在可以将本地的tomcat7和192.168.1.23:5000/tomcat7镜像都删除,然后7从私有仓库中下载 7 [root@localhost~]# docker rmi tomcat7 7[root@localhost~]# docker rmi 192.168.1.23:5000/tomcat7 8 7[root@localhost~]# docker pull 192.168.1.23:5000/tomcat7 9 [root@localhost~]# docker images 8REPOSITORY TAG 0 IMAGE 8ID CREATED SIZE 1 192.168.1.23:5000/tomcat7 latest 5hc8a2i8p413w 3 days ago 562.3 MB 2 8-------------------------------------------------------------------3 ---------- 8这样,也就可以在同一局域网内的其他机器上,从该私有仓库中pull下来该4 镜像。 8比如从192.168.1.17上拉取该私有仓库的tomcat镜像进行容器创建(注意,5 要在该机器的/etc/sysconfig/docker配置文件里添加8--insecure-registry=192.168.1.23:5000参数)详细见设置http访问仓库 6 [root@linux-node2 ~]# docker pull 192.168.1.23:5000/tomcat7 8 7 [root@linux-node2 ~]# docker images 8REPOSITORY TAG 68 IMAGE 8ID CREATED SIZE 9 192.168.1.23:5000/tomcat7 latest 2ec9e2e9b978a 3 days ago 562.3 MB 0 9这样就搭建了Docker私有仓库,上面搭建的仓库是不需要认证的,我们可以1 结合nginx和https实现认证和加密功能。 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109