Doker 在業界的應用越來越廣泛,怎么用戶管理好自己的鏡像、容器等就是一個迫在眉睫的任務。
由于業務需要,我們需要在搭建一套自己的 Docker 私有鏡像倉庫,網上找了很多,都是說要 pull 一個 regisitry 鏡像,然后通過這個鏡像啟動一個容器來運行倉庫應用,我按照官網的說明 pull 了一個 registry ,但是啟動的時候有報錯,具體是什么就不細說了,反正是有錯,于是開始研究別的方法,別說還真找到了一個,而且是我發現的最簡便的辦法,我不知道我是不是國內第一個發現的,但我應該是第一個寫出來給大家參考的。
下面不廢話,直接說方法:
下面就詳細列一下每一步的步驟:
使用 CentOS 7.X 系統,添加 epel 源,并更新系統到最新版本,重啟讓新的內核生效。
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo#yum clean all#yum makecache#yum update -y#reboot
安裝 docker 相關的服務,其中 docker-registry 這個最重要,因為這就是私有倉庫的服務,有了這個服務就不需要像網上一樣去 pull 鏡像,然后再起一個容器。
#yum install docker docker-registry -y
如果不需要開發相關的接口調用程序,這兩個就夠了,如果需要開發就直接安裝所有的 docker 包,一共也沒幾個。但是最好把 docker-latest 和 docker-latest-logrotate 兩個包卸載掉,因為這倆是 docker 客戶端,版本是 1.12 跟 server 的版本 1.10 不是太匹配。
#yum install docker* -y#yum remove docker-latest* -y
把 docker 的兩個服務設置為自動啟動,并讓其運行。
#systemctl enable docker#systemctl start docker#systemctl enable docker-registry#systemctl start docker-registry
查看一下本機監聽的端口,是不是有5000這個端口了?5000端口就是默認的 docker-registry 監聽端口,當然,這個你可以根據自己喜歡進行修改。
[root@01 /]# netstat -tnlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1109/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1384/mastertcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 20437/pythontcp6 0 0 :::22 :::* LISTEN 1109/sshdtcp6 0 0 ::1:25 :::* LISTEN 1384/master
測試一下是不是能通過網絡進行訪問了?
[root@01 /]# curl "http://192.168.1.107:5000""//"docker-registry server//""[root@01 /]#[root@01 /]#
既然可以訪問了,那就往這上面 push 一個鏡像來測試一下吧。這個需要你首先 tag 一個鏡像,然后才能 push 上去。以我目前的已經有的鏡像為例。
[root@01 /]# docker tag cfba59e097ba 192.168.1.107:5000/test1[root@01 /]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE192.168.1.107:5000/test1 latest ac0b483c17fa 3 days ago 634.6 MBdocker.io/redmine latest cfba59e097ba 3 days ago 634.6 MBdocker.io/registry latest c9bd19d022f6 2 weeks ago 33.27 MB[root@01 /]#
現在 push 的話有報錯,如下。
[root@01 /]# docker pull 192.168.1.107:5000/test1Using default tag: latestTrying to pull repository 192.168.1.107:5000/test1 ...unable to ping registry endpoint https://192.168.1.107:5000/v0/v2 ping attempt failed with error: Get https://192.168.1.107:5000/v2/: EOF v1 ping attempt failed with error: Get https://192.168.1.107:5000/v1/_ping: EOF[root@01 /]#
但是基本上一眼就能看出來,地址里都是 https,而我現在能訪問的只是 http,所以,就需要解決啟用 http 的問題,因為我的需求是在內網里搭建,外網無法訪問,何必要加密,只會拖慢速度。接下來就是修改對應的配置文件,啟用 http ,這個配置文件也是有說這個有說那個的,下面的才是正確的配置文件,親測有效,如下。
[root@01 /]# vim /etc/sysconfig/docker
把下面這一行添加進去。
OPTIONS='--insecure-registry 192.168.1.107:5000'
重啟 docker 服務。
[root@01 /]# systemctl restart docker docker-registry
再次 push,成功完成。
[root@01 system]# docker push 192.168.1.107:5000/test1The push refers to a repository [192.168.1.107:5000/test1]07c28c5d0371: Image successfully pushed6365a80ad26a: Image successfully pushedc5e7c0f1d017: Image successfully pushedb45f06d28f46: Image successfully pushed3f3c0394ba5a: Image successfully pushedddd6e2a8209e: Image successfully pushedf306cb9361f7: Image successfully pushed2d143a3783bc: Image successfully pushedf110684b8ae3: Image successfully pushedd7d24df90586: Image successfully pushede26addf75a78: Image successfully pushed82c666956815: Image successfully pushed9a2b1c643e93: Image successfully pushedeb9546f264dc: Image successfully pushedf96222d75c55: Image successfully pushedPushing tag for rev [cfba59e097ba] on {http://192.168.1.107:5000/v1/repositories/test1/tags/latest}[root@01 system]#
既然成功了,就往下 pull 一下試試,看看能不能讓別的機器用,結果當然也是成功的,因為我已經 pull 過了,所以顯示鏡像已經存在,如下。
[root@01 /]# docker pull 192.168.1.107:5000/test1Using default tag: latestTrying to pull repository 192.168.1.107:5000/test1 ...Pulling repository 192.168.1.107:5000/test1cfba59e097ba: Already existsf96222d75c55: Already existsd17727727b61: Already exists92db66c8ffce: Already exists10a436a2f8fa: Already exists8b40995a66da: Already existsa2cba87d9ea4: Already exists5a187c7a57c4: Already existsd15f50d30606: Already exists4366383cdf86: Already existsc7cb938f30c3: Already existsf135d604f740: Already exists3f3d23c69aef: Already existse6adcc9c0e4b: Already exists53289b480679: Already existsStatus: Image is up to date for 192.168.1.107:5000/test1:latest192.168.1.107:5000/test1: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker.[root@01 /]#
至此,簡單的私有倉庫已經搭建完畢,后續如果有需求要在公網上提供服務的話,加 SSL 證書,加用戶名/密碼等操作按部就班地去完成就行了。IT 技術更新很快,可能之前還沒有這個服務,只是最近才有,所以,選了這一行就需要一輩子不斷學習不斷進步才能站在橋頭迎風斬浪。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答
圖片精選