docker搭建私有仓库 妖狐艹你老母 2022-06-18 07:29 256阅读 0赞 注意:防火墙问题 docker搭建私有云 Dockers不仅提供了一个中央仓库,同时也允许我们使用registry搭建本地私有仓库。 使用私有仓库有许多优点: 1. 节省网络带宽,针对于每个镜像不用每个人都去中央仓库上面去下载,只需要从私有仓库中下载即可; 2. 提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,以供公司内部相关人员使用 注意: 2、关闭本地防火墙并设置开机不自启动 <table style="font-size: 12px; border-collapse: collapse; margin-top: 10px; margin-bottom: 10px; background-color: inherit;"> <tbody style="background-color: inherit; font-size: 1em !important;"> <tr style="background-color: inherit; font-size: 1em !important;"> <td style="word-break: break-all; border: 1px solid rgb(153, 153, 153); padding: 5px 16px 5px 12px; min-height: 25px; min-width: 25px; height: 26px; background-color: inherit; width: 692px; font-size: 1em !important;"> <div style="min-width: 2px; background-color: inherit; font-size: 1em !important;"> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: rgb(0, 130, 0) !important; font-size: 1em !important; background-color: inherit;"> # systemctl stop firewalld.service</code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: rgb(0, 130, 0) !important; font-size: 1em !important; background-color: inherit;"> # systemctl disable firewalld.service</code> </div> </div> </td> </tr> </tbody> </table> 3、关闭本地selinux防火墙 <table style="font-size: inherit; border-collapse: collapse; margin-top: 10px; margin-bottom: 10px; background-color: inherit;"> <tbody style="background-color: inherit;"> <tr style="background-color: inherit;"> <td style="word-break: break-all; border: 1px solid rgb(153, 153, 153); padding: 5px 16px 5px 12px; min-height: 25px; min-width: 25px; height: 25px; background-color: inherit;"> <div style="min-width: 2px; background-color: inherit;"> <br style="background-color: inherit;"> </div> </td> <td style="word-break: break-all; border: 1px solid rgb(153, 153, 153); padding: 5px 16px 5px 12px; min-height: 25px; min-width: 25px; height: 26px; background-color: inherit; width: 692px; font-size: 1em !important;"> <div style="min-width: 2px; background-color: inherit; font-size: 1em !important;"> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: rgb(0, 130, 0) !important; font-size: 1em !important; background-color: inherit;"> # vi /etc/sysconfig/selinux </code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; font-size: 1em !important; background-color: inherit;"> SELINUX=disabled</code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: rgb(0, 130, 0) !important; font-size: 1em !important; background-color: inherit;"> # setenforce 0 </code> </div> </div> </td> </tr> </tbody> </table> 一 环境准备 环境:两个装有Docker的Ubuntu虚拟机 虚拟机一:192.168.112.132 用户开发机 虚拟机二:192.168.112.136 用作私有仓库 此处我们准备了两个虚拟机,分别都安装了Docker,其中132机器用作开发机,136机器用作registry私有仓库机器。环境准备好之后接下来我们就开始搭建私有镜像仓库。 -------------------- # **搭建私有仓库** # 首先在136机器上下载registry镜像 $ sudo docker pull registry 下载完之后我们通过该镜像启动一个容器 $ sudo docker run -d -p 5000:5000 registry 默认情况下,会将仓库存放于容器内的/tmp/registry目录下,这样如果容器被删除,则存放于容器中的镜像也会丢失,所以我们一般情况下会指定本地一个目录挂载到容器内的/tmp/registry下,如下: $ sudo docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry ![这里写图片描述][Image 1] 可以看到我们启动了一个容器,地址为:192.168.112.136:5000。 -------------------- # **测试** # 接下来我们就要操作把一个本地镜像push到私有仓库中。首先在132机器下pull一个比较小的镜像来测试(此处使用的是busybox)。 $ sudo docker pull busybox ![这里写图片描述][Image 1] 接下来修改一下该镜像的tag。 $ sudo docker tag busybox 192.168.112.136:5000/busybox ![这里写图片描述][Image 1] 接下来把打了tag的镜像上传到私有仓库。 $ sudo docker push 192.168.112.136:5000/busybox ![这里写图片描述][Image 1] 可以看到push失败,具体错误如下: 2015/01/0511:01:17 Error: Invalid registry endpoint https://192.168.112.136:5000/v1/: Get https://192.168.112.136:5000/v1/_ping: dial tcp 192.168.112.136:5000: connection refused. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.112.136:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.112.136:5000/ca.crt * 1 因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。修改docker启动配置文件(此处是修改132机器的配置)Ubuntu下配置文件地址为:/etc/init/docker.conf,在其中增加–insecure-registry 192.168.112.136:5000如下所示: $ sudo vi /etc/init/docker.conf ![这里写图片描述][Image 1] 在centos7中修改文件 <table style="font-size: inherit; border-collapse: collapse; margin-top: 10px; margin-bottom: 10px; background-color: inherit;"> <tbody style="background-color: inherit; color: rgb(44, 44, 44); font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace; font-size: 1em !important;"> <tr style="background-color: inherit; font-size: 1em !important;"> <td style="word-break: break-all; border: 1px solid rgb(153, 153, 153); padding: 5px 16px 5px 12px; min-height: 25px; min-width: 25px; height: 26px; background-color: inherit; width: 692px; font-size: 1em !important;"> <div style="min-width: 2px; background-color: inherit; font-size: 1em !important;"> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;"> [root@localhost ~]</code> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: rgb(0, 130, 0) !important; font-size: 1em !important; background-color: inherit;"># vim /etc/sysconfig/docker</code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;"> 修改此行</code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;"> OPTIONS=</code> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: blue !important; font-size: 1em !important; background-color: inherit;">'--selinux-enabled --insecure-registry 192.168.0.109:5000'</code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;"> [root@localhost ~]</code> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: rgb(0, 130, 0) !important; font-size: 1em !important; background-color: inherit;"># service docker restart</code> </div> <div style="background-color: inherit; font-size: 1em !important;"> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;"> Redirecting to </code> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;">/bin/systemctl</code> <code style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace !important; color: black !important; font-size: 1em !important; background-color: inherit;">restart docker.service</code> </div> </div> </td> </tr> </tbody> </table> ![docker搭建私有仓库][Image 1] 修改完之后,重启Docker服务。 service docker restart ![这里写图片描述][Image 1] 重启完之后我们再次运行推送命令,把本地镜像推送到私有服务器上。 docker push 192.168.112.136:5000/busybox ![这里写图片描述][Image 1] 可以看到镜像已经push到私有仓库中去了。 接下来我们删除本地镜像,然后从私有仓库中pull下来该镜像。 $ sudo docker pull 192.168.112.136:5000/busybox ![这里写图片描述][Image 1] 到此就搭建好了Docker私有仓库。上面搭建的仓库是不需要认证的,我们可以结合nginx和https实现认证和加密功能。 # **管理仓库中的镜像** # -------------------- ### **查询** ### 如果我们想要查询私有仓库中的所有镜像,使用`docker search`命令: # docker search registry_ip:5000/ 如果要查询仓库中指定账户下的镜像,则使用如下命令: # docker search registry_ip:5000/account/ 同时也可以指定镜像查询。 ### **删除** ### 目前尚未找到方法删除私有仓库中的镜像,尝试过直接从仓库存储目录中删除镜像文件,但是并不能成功删除镜像。 # **Registry V2** # -------------------- Registry V2.2的安装脚本如下: #!/bin/bash# Description: create a private registry v2.2# Version: 0.2## Author: wangtao 479021795@qq.com# Date: 2015/10/29set -o xtrace if [[ $UID -ne 0 ]]; thenecho"Not root user. Please run as root."exit0fi# Install Docker if not docker -v if [[ $? -ne 0 ]]; thenecho"Please install Docker first."exit0fi REGISTRY_VERSION=2.2# Download registry image v2.2 docker pull registry:${REGISTRY_VERSION}# Start registry container mkdir /opt/registry docker run -d -p 5000:5000 --restart=always -v /opt/registry:/var/lib/registry --name hummer_registry registry:${REGISTRY_VERSION} * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 * 25 * 26 * 27 * 28 * 29 * 30 Registry的存放目录在Docker Hub上显示的是/tmp/registry-dev,但是映射之后发现并没有存放在该目录,查看源码发现,镜像信息存放在/var/lib/registry目录下,因此这里修改为将/opt/registry目录映射到/var/lib/registry。 **上传镜像** # docker push registry:5000/image_name **查看镜像** # curl -XGET http://registry:5000/v2/_catalog # curl -XGET http://registry:5000/v2/image_name/tags/list **删除镜像** 虽然看了官方API,但是还是不能成功删除,不知道digest如何生成。如果有知道的同学请不吝赐教。[Docker Registry HTTP API V2][] 根据网上资料显示,当前版本尚不支持该功能,再等等看新版本吧。 -------------------- 转载自:[http://blog.csdn.net/u010397369/article/details/42422243][http_blog.csdn.net_u010397369_article_details_42422243] 根据自己的实验有所改动。 本来自己之前已经写了一篇关于Docker私有仓库的搭建的文章,但是后来不小心把它给覆盖了,也不想再写一遍了。这篇文章跟我的搭建思路差不多,所以就转载了这篇文章。 添加了Registry v2的安装部分。 [Image 1]: [Docker Registry HTTP API V2]: https://docs.docker.com/registry/spec/api/#deleting-an-image [http_blog.csdn.net_u010397369_article_details_42422243]: http://blog.csdn.net/u010397369/article/details/42422243
还没有评论,来说两句吧...