隨著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 /etc/docker/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 /etc/docker/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 /etc/docker/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
到此這篇關于Docker私有倉庫Registry部署的實現的文章就介紹到這了,更多相關Docker私有倉庫Registry內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!