Docker容器学习梳理--私有仓库Registry使用

但有时候使用Docker Hub这样的公共仓库可能不方便,这种情况下用户可以使用registry创建一个本地仓库供私人使用,这点跟Maven的管理类似。
使用私有仓库有许多优点:

1
2
1)节省网络带宽,针对于每个镜像不用每个人都去中央仓库上面去下载,只需要从私有仓库中下载即可;
2)提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,以供公司内部相关人员使用。

目前Docker Registry已经升级到了v2,最新版的Docker已不再支持v1。Registry v2使用Go语言编写,在性能和安全性上做了很多优化,重新设计了镜像的存储格式。如果需要安装registry v2,只需下载registry:2.2即可。Docker官方提供的工具docker-registry可以用于构建私有的镜像仓库。废话不多说了,下面记录下Docker私有仓库构建的过程:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
选择一台服务器(内外网地址: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配置文件中添加。
我这里测试用的是同一台机器,即将注册机192.168.1.23本机的镜像上传到它的仓库内。
-----------------------------------------------------------------------------------------------------------
[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实现认证和加密功能。

注意查看镜像方法(docker pull registry:2.1.1):

1
2
# curl -XGET http://registry_ip:5000/v2/_catalog
# curl -XGET http://registry_ip:5000/v2/image_name/tags/list


出处:https://www.cnblogs.com/kevingrace/p/6628062.html

猜你喜欢

转载自blog.csdn.net/jackliu16/article/details/80515051