隨著docker使用的鏡像越來越多,就需要有一個(gè)保存鏡像的地方,這就是倉(cāng)庫(kù)。目前常用的兩種倉(cāng)庫(kù):公共倉(cāng)庫(kù)和私有倉(cāng)庫(kù)。最方便的就是使用公共倉(cāng)庫(kù)上傳和下載,下載公共倉(cāng)庫(kù)的鏡像是不需要注冊(cè)的,但是上傳時(shí),是需要注冊(cè)的。
私有倉(cāng)庫(kù)最常用的就是Registry、Harbor兩種,那接下來詳細(xì)介紹如何搭建registry私有倉(cāng)庫(kù),Harbor將在下一篇博文部署。
一、部署Registry私有倉(cāng)庫(kù)
案例描述
兩臺(tái)CentOS7.4,一臺(tái)為Docker私有倉(cāng)庫(kù);另一臺(tái)為Docker客戶端,測(cè)試使用;
兩臺(tái)服務(wù)器都需要安裝Docker服務(wù),請(qǐng)參考博文:安裝Docker.v19版本
1、配置registry私有倉(cāng)庫(kù)
[root@centos01 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
<!--docker宿主機(jī)開啟路由功能-->
[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 <!--重新啟動(dòng)docker服務(wù)-->
[root@centos01 ~]# docker search registry <!--查找registry鏡像-->
<!--registry鏡像可以直接先pull下來,也可以不下載,根據(jù)自己情況而定-->
[root@centos01 ~]# docker run -d -p 5000:5000 --name registry --restart=always -v /opt/registry:/var/lib/registry registry
<!--運(yùn)行registry容器,運(yùn)行registry服務(wù)存儲(chǔ)自己的鏡像-->
<!--"--restart=always"參數(shù)是指此容器跟隨docker服務(wù)啟動(dòng)而啟動(dòng)-->
[root@centos01 ~]# docker ps <!--查看docker運(yùn)行的容器-->
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服務(wù)支持registry服務(wù)-->
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"insecure-registries":["192.168.100.10:5000"] <!--添加此行-->
}
[root@centos01 ~]# systemctl reload docker <!--重新啟動(dòng)docker服務(wù)-->
2、上傳鏡像到registry私有倉(cāng)庫(kù)
[root@centos01 ~]# docker tag hub.c.163.com/public/centos:6.7-tools 192.168.100.10:5000/image/centos:6.7
<!--修改鏡像標(biāo)簽-->
[root@centos01 ~]# docker push 192.168.100.10:5000/image/centos:6.7 <!--上傳鏡像到registry私有倉(cāng)庫(kù)-->
二、配置Docker客戶端訪問私有倉(cāng)庫(kù)
<!--客戶端安裝docker服務(wù),配置鏡像加速-->
[root@centos02 ~]# vim /etc/docker/daemon.json <!--配置docker支持registry服務(wù) -->
{"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"],
"insecure-registries":["192.168.100.10:5000"] <!--添加此行-->
}
[root@centos02 ~]# systemctl restart docker <!--重新啟動(dòng)docker服務(wù)-->
[root@centos02 ~]# docker pull 192.168.100.10:5000/image/centos:6.7
<!--客戶端下載私有倉(cāng)庫(kù)中的鏡像-->
[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私有倉(cāng)庫(kù)已經(jīng)搭建完成,但是現(xiàn)在存在一個(gè)問題,如果這也部署的話企業(yè)內(nèi)部所有人員皆可訪問我們的私有倉(cāng)庫(kù),為了安全起見,接下來為registry添加一個(gè)身份驗(yàn)證,只有通過了身份驗(yàn)證才可以上傳或者下載私有倉(cāng)庫(kù)中的鏡像。
三、配置registry加載身份驗(yàn)證
[root@centos01 ~]# yum -y install httpd-tools <!--安裝加密工具h(yuǎn)ttpd-tools-->
[root@centos01 ~]# mkdir /opt/registry-auth <!--創(chuàng)建存放驗(yàn)證密鑰目錄-->
[root@centos01 ~]# htpasswd -Bbn bob pwd@123 > /opt/registry-auth/htpasswd
<!--配置registry身份驗(yàn)證數(shù)據(jù)庫(kù)-->
<!--"-Bbn”參數(shù)解釋:B強(qiáng)制密碼加密;b在命令中輸入密碼,不提示輸入密碼;n不更新密鑰文件-->
<!--刪除此服務(wù)器上的所有容器,接下來重新生成一個(gè)需要身份驗(yàn)證的私有倉(cāng)庫(kù)容器-->
[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
<!--重新運(yùn)行一個(gè)支持身份驗(yàn)證的registry私有鏡像倉(cāng)庫(kù)容器-->
[root@centos01 ~]# docker tag tomcat:latest 192.168.100.10:5000/image/tomcat:1.0
<!--鏡像修改標(biāo)簽-->
[root@centos01 ~]# docker push 192.168.100.10:5000/image/tomcat:1.0
<!--測(cè)試不通過身份驗(yàn)證是否可以往私有倉(cāng)庫(kù)上傳鏡像-->
no basic auth credentials
<!--提示沒有身份驗(yàn)證,上傳不了-->
[root@centos01 ~]# docker login 192.168.100.10:5000
<!--登錄私有鏡像倉(cāng)庫(kù),通過身份驗(yàn)證即可上傳-->
Username: bob <!--輸入bob-->
Password: <!--輸入密碼-->
……………… <!--此處省略部分內(nèi)容-->
Login Succeeded <!--已通過身份驗(yàn)證,此時(shí)可以上傳鏡像到私有倉(cāng)庫(kù)-->
[root@centos01 ~]# docker push 192.168.100.10:5000/image/tomcat:1.0 <!--再次上傳鏡像到私有倉(cāng)庫(kù)-->
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客戶端不通過身份驗(yàn)證直接下載私有倉(cāng)庫(kù)中的鏡像直接被拒絕-->
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
<!--登錄私有倉(cāng)庫(kù),通過身份驗(yàn)證-->
Username: bob <!--輸入bob-->
Password: <!--輸入密碼-->
Login Succeeded <!--通過身份驗(yàn)證-->
[root@centos02 ~]# docker pull 192.168.100.10:5000/image/tomcat:1.0 <!--下載私有倉(cāng)庫(kù)中的鏡像-->
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
到此這篇關(guān)于Docker私有倉(cāng)庫(kù)Registry部署的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Docker私有倉(cāng)庫(kù)Registry內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!