Docker私有仓库概述与部署环境
随着Docker镜像数量的增加,集中存储和管理镜像成为必要,这便引出了仓库的概念。当前主流的仓库类型分为公共仓库和私有仓库。使用公共仓库进行镜像的拉取无需注册账号,但若要推送镜像则必须进行注册登录。
在私有仓库解决方案中,Registry和Harbor是最为常见的选择。本文将重点阐述如何搭建基于Registry的私有仓库,而Harbor的详细部署将在后续文章中介绍。
一、部署Registry私有仓库
案例描述
本案例涉及两台运行CentOS 7.4操作系统的服务器:一台作为Docker私有仓库的服务端,另一台作为用于测试的Docker客户端。
这两台服务器均需预先安装Docker服务,具体安装步骤可参考相关博文《安装Docker.v19版本》。
1、配置registry私有仓库
[root@centos01 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@centos01 ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@centos01 ~]# vim /etc/docker/daemon.json
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"]}
[root@centos01 ~]# systemctl reload docker
[root@centos01 ~]# docker search registry
[root@centos01 ~]# docker run -d -p 5000:5000 --name registry --restart=always -v /opt/registry:/var/lib/registry registry
[root@centos01 ~]# docker ps
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
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 /etc/docker/daemon.json
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"insecure-registries":["192.168.100.10:5000"]
}
[root@centos01 ~]# systemctl reload 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
二、配置Docker客户端访问私有仓库
[root@centos02 ~]# vim /etc/docker/daemon.json
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"insecure-registries":["192.168.100.10:5000"]
}
[root@centos02 ~]# systemctl restart 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
[root@centos01 ~]# mkdir /opt/registry-auth
[root@centos01 ~]# htpasswd -Bbn bob pwd@123 > /opt/registry-auth/htpasswd
[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
[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
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
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
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
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
关于Docker私有仓库Registry部署的实现内容到此结束,更多相关Docker私有仓库Registry内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!