选择一台服务器(内外网地址:192.168.1.23)作为注册服务器,用于搭建私有仓库。(该机器要安装了Docker环境) 1)从Docker官方仓库里下载registry镜像 [root@localhost ~] # docker pull registry:2.2 ---------------------------------------------------------------- 或者: [root@localhost ~] # docker pull registry [root@localhost ~] # docker pull registry:2.1.1 ---------------------------------------------------------------- 下载完之后,可以通过该镜像启动一个容器 [root@localhost ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat7 latest 97c6a43dd69c 12 minutes ago 562.3 MB docker.io /registry 2.2 ad379b517aa6 14 months ago 224.5 MB 默认情况下,会将私有仓库存放于容器内的 /tmp/registry 目录下,这样如果容器被删除,则存放于容器中的镜像也会丢失。 所以一般情况下会指定本地一个目录挂载到容器内的 /tmp/registry 下,如下: [root@localhost ~] # docker run -d --name=my_registry -p 5000:5000 -v /opt/data/registry:/tmp/registry docker.io/registry:2.2 9fe45329bda17f61da04e6e8d2faf124fb22665a25270421bb8979a419809446 [root@localhost ~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d8e98b1068cd docker.io /registry :2.2 "/bin/registry /etc/d" About a minute ago Up About a minute 0.0.0.0:5000->5000 /tcp my_registry 由上可以看到,已经启动了一个容器,地址为:192.168.1.23:5000。 2)测试 接下来可以把一个本地镜像push(如下面的tomcat7镜像)到私有仓库中。 [root@localhost ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat7 latest 97c6a43dd69c 18 minutes ago 562.3 MB docker.io /registry 2.2 ad379b517aa6 14 months ago 224.5 MB 修改一下该镜像的tag标识。 [root@localhost ~] # docker tag tomcat7 192.168.1.23:5000/tomcat7 [root@localhost ~] # docker tag tomcat7 192.168.1.23:5000/tomcat7 //修改了tag后的镜像若要删除,docker rmi后面不能用镜像ID了,需要用docker rmi 192.168.1.23:5000/tomcat7:latest REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.1.23:5000 /tomcat7 latest 97c6a43dd69c 18 minutes ago 562.3 MB tomcat7 latest 97c6a43dd69c 18 minutes ago 562.3 MB docker.io /registry 2.2 ad379b517aa6 14 months ago 224.5 MB 接下来把上面修改tag后的镜像上传到私有仓库。 [root@localhost ~] # docker push 192.168.1.23:5000/tomcat7 The push refers to a repository [192.168.1.23:5000 /tomcat7 ] unable to ping registry endpoint https: //192 .168.1.23:5000 /v0/ v2 ping attempt failed with error: Get https: //192 .168.1.23:5000 /v2/ : http: server gave HTTP response to HTTPS client v1 ping attempt failed with error: Get https: //192 .168.1.23:5000 /v1/_ping : http: server gave HTTP response to HTTPS client 出现上面错误的原因分析: 因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。 为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。 需要在docker的配置文件 /etc/sysconfig/docker (ubuntu系统中的docker配置文件时 /etc/default/docker )添加参数“--insecure-registry=192.168.1.23:5000”。 ----------------------------------------------------------------------------------------------------------- 温馨提示: 这个是在客户机的docker配置文件里添加的(即上传镜像到私有仓库里或从私有仓库下载镜像的客户机)。 比如说在A机器上将它的镜像上传到192.168.1.23的私有仓库上或从该私有仓库下载镜像,那么就在A机器的本地docker配置文件中添加。 我这里测试用的是同一台机器(centos7),即将注册机192.168.1.23本机的镜像上传到它的仓库内。 [如果是centos6系统,就在other_args选项后面添加。other_args= "--graph=/docker --insecure-registry=192.168.1.234:5000" ] ----------------------------------------------------------------------------------------------------------- [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@localhost ~] # service docker restart 由于docker服务重启后,所有容器都会被关闭。所以需要在docker重启后再次启动容器 [root@localhost ~] # docker start my_registry my_registry [root@localhost ~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d8e98b1068cd docker.io /registry :2.2 "/bin/registry /etc/d" About a minute ago Up About a minute 0.0.0.0:5000->5000 /tcp my_registry 再次提交到私有仓库 [root@localhost ~] # docker push 192.168.1.23:5000/tomcat7 The push refers to a repository [192.168.1.23:5000 /tomcat7 ] c6d7ce9e90d7: Pushed 34e7b85d83e4: Pushed latest: digest: sha256:5fdcbaf254cb44dd26645f606cccea8de76118baff03485e40853c691a15956d size: 720 上面命令执行无误后,就表示镜像已经push到私有仓库中去了。 查看私有仓库里的镜像(一定要保证下面能查看到仓库里有镜像!如果仓库里没有镜像,那么客户端机器就无法从该私有仓库下载镜像了) [root@localhost ~] # curl -XGET http://192.168.1.23:5000/v2/_catalog //即该私有仓库里有tomcat7镜像 { "repositories" :[ "tomcat7" ]} [root@localhost ~] # curl -XGET http://192.168.1.23:5000/v2/tomcat7/tags/list { "name" : "tomcat7" , "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 现在可以将本地的tomcat7和192.168.1.23:5000 /tomcat7 镜像都删除,然后从私有仓库中下载 [root@localhost ~] # docker rmi tomcat7 [root@localhost ~] # docker rmi 192.168.1.23:5000/tomcat7 [root@localhost ~] # docker pull 192.168.1.23:5000/tomcat7 [root@localhost ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.1.23:5000 /tomcat7 latest 5hc8a2ip413w 3 days ago 562.3 MB ----------------------------------------------------------------------------- 这样,也就可以在同一局域网内的其他机器上,从该私有仓库中pull下来该镜像。 比如在192.168.1.17服务器上拉取该私有仓库的tomcat镜像进行容器创建(注意,要在该机器的 /etc/sysconfig/docker 配置文件里添加--insecure-registry=192.168.1.23:5000参数) [root@linux-node2 ~] # docker pull 192.168.1.23:5000/tomcat7 [root@linux-node2 ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.1.23:5000 /tomcat7 latest 2ec9e2eb978a 3 days ago 562.3 MB 这样就搭建了Docker私有仓库,上面搭建的仓库是不需要认证的,我们可以结合nginx和https实现认证和加密功能。 |