详解Docker私有仓库最简便的搭建方法

本篇文章主要介绍了Docker私有仓库最简便的搭建方法,具有一定的参考价值,有兴趣的可以了解一下。

Doker 在业界的应用越来越广泛,怎么用户管理好自己的镜像、容器等就是一个迫在眉睫的任务。

由于业务需要,我们需要在搭建一套自己的 Docker 私有镜像仓库,网上找了很多,都是说要 pull 一个 regisitry 镜像,然后通过这个镜像启动一个容器来运行仓库应用,我按照官网的说明 pull 了一个 registry ,但是启动的时候有报错,具体是什么就不细说了,反正是有错,于是开始研究别的方法,别说还真找到了一个,而且是我发现的最简便的办法,我不知道我是不是国内第一个发现的,但我应该是第一个写出来给大家参考的。

下面不废话,直接说方法:

  • 首先,你的系统要是 CentOS 7.0 以上,因为内核的要求,以及各种相关的库和软件的需要,以及 epel 的需要。
  • 直接安装 docker-registry 这个包。
  • 稍微修改一下配置,让你的私有仓库支持 http,因为从 docker1.3.2 开始,docker registry 默认都是使用 https 协议而不使用 http,甭管你从 docker hub 上找你需要的镜像,还是你自己打出来的 private registry。
  • 重启相关的 docker 服务。
  • 测试及使用。

下面就详细列一下每一步的步骤:

使用 CentOS 7.X 系统,添加 epel 源,并更新系统到最新版本,重启让新的内核生效。

 #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #yum clean all #yum makecache #yum update -y #reboot

安装 docker 相关的服务,其中 docker-registry 这个最重要,因为这就是私有仓库的服务,有了这个服务就不需要像网上一样去 pull 镜像,然后再起一个容器。

 #yum install docker docker-registry -y

如果不需要开发相关的接口调用程序,这两个就够了,如果需要开发就直接安装所有的 docker 包,一共也没几个。但是最好把 docker-latest 和 docker-latest-logrotate 两个包卸载掉,因为这俩是 docker 客户端,版本是 1.12 跟 server 的版本 1.10 不是太匹配。

 #yum install docker* -y #yum remove docker-latest* -y

把 docker 的两个服务设置为自动启动,并让其运行。

 #systemctl enable docker #systemctl start docker #systemctl enable docker-registry #systemctl start docker-registry

查看一下本机监听的端口,是不是有5000这个端口了?5000端口就是默认的 docker-registry 监听端口,当然,这个你可以根据自己喜欢进行修改。

 [root@01 /]# netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address   Foreign Address   State  PID/Program name tcp  0  0 0.0.0.0:22    0.0.0.0:*    LISTEN  1109/sshd tcp  0  0 127.0.0.1:25   0.0.0.0:*    LISTEN  1384/master tcp  0  0 0.0.0.0:5000   0.0.0.0:*    LISTEN  20437/python tcp6  0  0 :::22     :::*     LISTEN  1109/sshd tcp6  0  0 ::1:25     :::*     LISTEN  1384/master

测试一下是不是能通过网络进行访问了?

 [root@01 /]# curl "http://192.168.1.107:5000" "\\"docker-registry server\\""[root@01 /]# [root@01 /]#

既然可以访问了,那就往这上面 push 一个镜像来测试一下吧。这个需要你首先 tag 一个镜像,然后才能 push 上去。以我目前的已经有的镜像为例。

 [root@01 /]# docker tag cfba59e097ba 192.168.1.107:5000/test1 [root@01 /]# docker images REPOSITORY     TAG     IMAGE ID   CREATED    SIZE 192.168.1.107:5000/test1 latest    ac0b483c17fa  3 days ago   634.6 MB docker.io/redmine   latest    cfba59e097ba  3 days ago   634.6 MB docker.io/registry   latest    c9bd19d022f6  2 weeks ago   33.27 MB [root@01 /]#

现在 push 的话有报错,如下。

 [root@01 /]# docker pull 192.168.1.107:5000/test1 Using default tag: latest Trying to pull repository 192.168.1.107:5000/test1 ... unable to ping registry endpoint https://192.168.1.107:5000/v0/ v2 ping attempt failed with error: Get https://192.168.1.107:5000/v2/: EOF v1 ping attempt failed with error: Get https://192.168.1.107:5000/v1/_ping: EOF [root@01 /]#

但是基本上一眼就能看出来,地址里都是 https,而我现在能访问的只是 http,所以,就需要解决启用 http 的问题,因为我的需求是在内网里搭建,外网无法访问,何必要加密,只会拖慢速度。接下来就是修改对应的配置文件,启用 http ,这个配置文件也是有说这个有说那个的,下面的才是正确的配置文件,亲测有效,如下。

 [root@01 /]# vim /etc/sysconfig/docker

把下面这一行添加进去。

 OPTIONS='--insecure-registry 192.168.1.107:5000'

重启 docker 服务。

 [root@01 /]# systemctl restart docker docker-registry

再次 push,成功完成。

 [root@01 system]# docker push 192.168.1.107:5000/test1 The push refers to a repository [192.168.1.107:5000/test1] 07c28c5d0371: Image successfully pushed 6365a80ad26a: Image successfully pushed c5e7c0f1d017: Image successfully pushed b45f06d28f46: Image successfully pushed 3f3c0394ba5a: Image successfully pushed ddd6e2a8209e: Image successfully pushed f306cb9361f7: Image successfully pushed 2d143a3783bc: Image successfully pushed f110684b8ae3: Image successfully pushed d7d24df90586: Image successfully pushed e26addf75a78: Image successfully pushed 82c666956815: Image successfully pushed 9a2b1c643e93: Image successfully pushed eb9546f264dc: Image successfully pushed f96222d75c55: Image successfully pushed Pushing tag for rev [cfba59e097ba] on {http://192.168.1.107:5000/v1/repositories/test1/tags/latest} [root@01 system]# 

既然成功了,就往下 pull 一下试试,看看能不能让别的机器用,结果当然也是成功的,因为我已经 pull 过了,所以显示镜像已经存在,如下。

 [root@01 /]# docker pull 192.168.1.107:5000/test1 Using default tag: latest Trying to pull repository 192.168.1.107:5000/test1 ... Pulling repository 192.168.1.107:5000/test1 cfba59e097ba: Already exists f96222d75c55: Already exists d17727727b61: Already exists 92db66c8ffce: Already exists 10a436a2f8fa: Already exists 8b40995a66da: Already exists a2cba87d9ea4: Already exists 5a187c7a57c4: Already exists d15f50d30606: Already exists 4366383cdf86: Already exists c7cb938f30c3: Already exists f135d604f740: Already exists 3f3d23c69aef: Already exists e6adcc9c0e4b: Already exists 53289b480679: Already exists Status: Image is up to date for 192.168.1.107:5000/test1:latest 192.168.1.107:5000/test1: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker. [root@01 /]# 

至此,简单的私有仓库已经搭建完毕,后续如果有需求要在公网上提供服务的话,加 SSL 证书,加用户名/密码等操作按部就班地去完成就行了。IT 技术更新很快,可能之前还没有这个服务,只是最近才有,所以,选了这一行就需要一辈子不断学习不断进步才能站在桥头迎风斩浪。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是详解Docker私有仓库最简便的搭建方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 站长技巧