Docker私有仓库Registry部署的实现

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Docker私有仓库Registry部署的实现

俊伟祺i   2020-06-02 我要评论

随着docker使用的镜像越来越多,就需要有一个保存镜像的地方,这就是仓库。目前常用的两种仓库:公共仓库和私有仓库。最方便的就是使用公共仓库上传和下载,下载公共仓库的镜像是不需要注册的,但是上传时,是需要注册的。

私有仓库最常用的就是Registry、Harbor两种,那接下来详细介绍如何搭建registry私有仓库,Harbor将在下一篇博文部署。

一、部署Registry私有仓库

案例描述

两台CentOS7.4,一台为Docker私有仓库;另一台为Docker客户端,测试使用;

两台服务器都需要安装Docker服务,请参考博文:安装Docker.v19版本

1、配置registry私有仓库

[root@centos01 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf  
    <!--docker宿主机开启路由功能-->
[root@centos01 ~]# sysctl -p  <!--刷新配置-->
net.ipv4.ip_forward = 1
[root@centos01 ~]# vim /etchttps://img.qb5200.com/download-x/dockerhttps://img.qb5200.com/download-x/daemon.json  <!--配置镜像加速-->
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"]}  <!--添加阿里云加速-->
[root@centos01 ~]# systemctl reload docker <!--重新启动docker服务-->
[root@centos01 ~]# docker search registry <!--查找registry镜像-->
<!--registry镜像可以直接先pull下来,也可以不下载,根据自己情况而定-->
[root@centos01 ~]# docker run -d -p 5000:5000 --name registry --restart=always -v /opt/registry:/var/lib/registry registry
 <!--运行registry容器,运行registry服务存储自己的镜像-->
 <!--"--restart=always"参数是指此容器跟随docker服务启动而启动-->
[root@centos01 ~]# docker ps  <!--查看docker运行的容器-->
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
a7773d77b8a3    registry      "/entrypoint.sh /etc…"  50 seconds ago   Up 46 seconds    0.0.0.0:5000->5000/tcp  registry
[root@centos01 ~]# docker images  <!--查看docker所有镜像-->
REPOSITORY          TAG         IMAGE ID      CREATED       SIZE
registry           latest       708bc6af7e5e    3 months ago    25.8MB
tomcat            latest       1b6b1fe7261e    5 days ago     647MB
hub.c.163.com/public/centos  6.7-tools      b2ab0ed558bb    3 years ago     602MB
[root@centos01 ~]# vim /etchttps://img.qb5200.com/download-x/dockerhttps://img.qb5200.com/download-x/daemon.json <!--配置docker服务支持registry服务-->
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"insecure-registries":["192.168.100.10:5000"]  <!--添加此行-->
}
[root@centos01 ~]# systemctl reload docker  <!--重新启动docker服务-->

2、上传镜像到registry私有仓库

[root@centos01 ~]# docker tag hub.c.163.com/public/centos:6.7-tools 192.168.100.10:5000/image/centos:6.7  
    <!--修改镜像标签-->
[root@centos01 ~]# docker push 192.168.100.10:5000/image/centos:6.7 <!--上传镜像到registry私有仓库-->

二、配置Docker客户端访问私有仓库

<!--客户端安装docker服务,配置镜像加速-->
[root@centos02 ~]# vim /etchttps://img.qb5200.com/download-x/dockerhttps://img.qb5200.com/download-x/daemon.json  <!--配置docker支持registry服务 -->
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"insecure-registries":["192.168.100.10:5000"]  <!--添加此行-->
}
[root@centos02 ~]# systemctl restart docker  <!--重新启动docker服务-->
[root@centos02 ~]# docker pull 192.168.100.10:5000/image/centos:6.7 
         <!--客户端下载私有仓库中的镜像-->
[root@centos02 ~]# docker images <!--查看镜像是否下载成功-->
REPOSITORY             TAG         IMAGE ID      CREATED       SIZE
192.168.100.10:5000/image/centos  6.7         b2ab0ed558bb    3 years ago     602MB

至此registry私有仓库已经搭建完成,但是现在存在一个问题,如果这也部署的话企业内部所有人员皆可访问我们的私有仓库,为了安全起见,接下来为registry添加一个身份验证,只有通过了身份验证才可以上传或者下载私有仓库中的镜像。

三、配置registry加载身份验证

[root@centos01 ~]# yum -y install httpd-tools  <!--安装加密工具httpd-tools-->
[root@centos01 ~]# mkdir /opt/registry-auth <!--创建存放验证密钥目录-->
[root@centos01 ~]# htpasswd -Bbn bob pwd@123 > /opt/registry-auth/htpasswd
 <!--配置registry身份验证数据库-->
<!--"-Bbn”参数解释:B强制密码加密;b在命令中输入密码,不提示输入密码;n不更新密钥文件-->

<!--删除此服务器上的所有容器,接下来重新生成一个需要身份验证的私有仓库容器-->
[root@centos01 ~]# docker run -d -p 5000:5000 --restart=always \
-v /opt/registry-auth/:/auth/ \
-v /opt/registry:/var/lib/registry --name registry-auth -e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" registry 
 <!--重新运行一个支持身份验证的registry私有镜像仓库容器-->
[root@centos01 ~]# docker tag tomcat:latest 192.168.100.10:5000/image/tomcat:1.0 
    <!--镜像修改标签-->
[root@centos01 ~]# docker push 192.168.100.10:5000/image/tomcat:1.0 
<!--测试不通过身份验证是否可以往私有仓库上传镜像-->
no basic auth credentials
<!--提示没有身份验证,上传不了-->
[root@centos01 ~]# docker login 192.168.100.10:5000 
    <!--登录私有镜像仓库,通过身份验证即可上传-->
Username: bob   <!--输入bob-->
Password:    <!--输入密码-->
……………… <!--此处省略部分内容-->
Login Succeeded     <!--已通过身份验证,此时可以上传镜像到私有仓库-->
[root@centos01 ~]# docker push 192.168.100.10:5000/image/tomcat:1.0 <!--再次上传镜像到私有仓库-->
The push refers to repository [192.168.100.10:5000/image/tomcat]
b0ac242ce8d3: Pushed
5e71d8e4cd3d: Pushed
eb4497d7dab7: Pushed
bfbfe00b44fc: Pushed
d39111fb2602: Pushed
155d997ed77c: Pushed
88cfc2fcd059: Pushed
760e8d95cf58: Pushed
7cc1c2d7e744: Pushed
8c02234b8605: Pushed
1.0: digest: sha256:55b41e0290d32d6888aee2e9a15f03cc88d2f49d5ad68892c54b9527d0ed181c size: 2421
[root@centos02 ~]# docker pull 192.168.100.10:5000/image/tomcat:1.0 
 <!--docker客户端不通过身份验证直接下载私有仓库中的镜像直接被拒绝-->
Error response from daemon: Get http://192.168.100.10:5000/v2/image/tomcat/manifests/1.0: no basic auth credentials
[root@centos02 ~]# docker login 192.168.100.10:5000 
    <!--登录私有仓库,通过身份验证-->
Username: bob  <!--输入bob-->
Password:     <!--输入密码-->
Login Succeeded   <!--通过身份验证-->
[root@centos02 ~]# docker pull 192.168.100.10:5000/image/tomcat:1.0 <!--下载私有仓库中的镜像-->
1.0: Pulling from image/tomcat
376057ac6fa1: Pull complete
5a63a0a859d8: Pull complete
496548a8c952: Pull complete
2adae3950d4d: Pull complete
0a297eafb9ac: Pull complete
09a4142c5c9d: Pull complete
9e78d9befa39: Pull complete
18f492f90b9c: Pull complete
7834493ec6cd: Pull complete
216b2be21722: Pull complete
Digest: sha256:55b41e0290d32d6888aee2e9a15f03cc88d2f49d5ad68892c54b9527d0ed181c
Status: Downloaded newer image for 192.168.100.10:5000/image/tomcat:1.0
192.168.100.10:5000/image/tomcat:1.0
[root@centos02 ~]# docker images  <!--查看docker客户端镜像-->
REPOSITORY             TAG         IMAGE ID      CREATED       SIZE
192.168.100.10:5000/image/tomcat  1.0         1b6b1fe7261e    5 days ago     647MB
192.168.100.10:5000/image/centos  6.7         b2ab0ed558bb    3 years ago     602MB

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们