文章來源:centos7 Docker私有倉庫搭建及刪除鏡像html
若是不想用私有鏡像庫,你能夠用docker的庫 https://hub.docker.compython
環境:兩個裝有Docker 17.09.0-ce 的centos7虛擬機
虛擬機一:192.168.0.154 用戶開發機
虛擬機二:192.168.0.153 用做私有倉庫nginx
在153機器上下載registry鏡像web
docker pull registry
下載完以後咱們經過該鏡像啓動一個容器docker
docker run -d -p 5000:5000 registry
默認狀況下,會將倉庫存放於容器內的/tmp/registry目錄下,這樣若是容器被刪除,則存放於容器中的鏡像也會丟失,因此咱們通常狀況下會指定本地一個目錄掛載到容器內的/tmp/registry下,vim
不過具體的狀況仍是要到容器裏去看centos
先啓動容器服務器
docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry b4c21ca8cf8a23ea72e0471909742541ffc312ea5cf492486b5bdc3130179864
咱們從新啓動下 registrycurl
docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry -v /data/config.yml:/etc/docker/registry/config.yml registry
/data/config.yml 這個是什麼呢?咱們在下面刪除倉庫鏡像介紹tcp
這裏須要說明一點,在啓動倉庫時,需在配置文件中的storage配置中增長delete=true配置項,容許刪除鏡像。默認的鏡像是沒有這個參數
1 cat config.yml 2 3 version: 0.1 4 log: 5 fields: 6 service: registry 7 storage: 8 delete: 9 enabled: true 10 cache: 11 blobdescriptor: inmemory 12 filesystem: 13 rootdirectory: /var/lib/registry 14 http: 15 addr: :5000 16 headers: 17 X-Content-Type-Options: [nosniff] 18 health: 19 storagedriver: 20 enabled: true 21 interval: 10s 22 threshold: 3
能夠看到咱們啓動了一個容器,地址爲:192.168.0.153:5000。
接下來咱們就要操做把一個本地鏡像push到私有倉庫中。首先在153機器下pull一個比較小的鏡像來測試(此處使用的是busybox)。
docker pull busybox
接下來修改一下該鏡像的tag。
docker tag busybox 192.168.0.153:5000/busybox
接下來把打了tag的鏡像上傳到私有倉庫。
docker push 192.168.0.153:5000/busybox
能夠看到 push 失敗:
Error: Invalid registry endpoint https://192.168.0.153:5000/v1/: Get https://192.168.0.153:5000/v1/_ping: dial tcp 192.168.0.153:5000: connection refused. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.112.136:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.0.153:5000/ca.crt
由於Docker從1.3.X以後,與docker registry交互默認使用的是https,然而此處搭建的私有倉庫只提供http服務,因此當與私有倉庫交互時就會報上面的錯誤。爲了解決這個問題須要在啓動docker server時增長啓動參數爲默認使用http訪問。修改docker啓動配置文件:
vim /usr/lib/systemd/system/docker.service
找到 ExecStart
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.0.153:5000
紅色字體爲添加的
重啓docker:
1 systemctl daemon-reload 2 systemctl restart docker
重啓完以後咱們再次運行推送命令,把本地鏡像推送到私有服務器上。
docker push 192.168.0.153:5000/busybox
接下來咱們從私有倉庫中pull下來該鏡像。
sudo docker pull 192.168.0.153:5000/busybox
查看鏡像
1 # curl -XGET http://registry:5000/v2/_catalog 2 # curl -XGET http://registry:5000/v2/image_name/tags/list
Docker倉庫在2.1版本中支持了刪除鏡像的API,但這個刪除操做只會刪除鏡像元數據,不會刪除層數據。在2.4版本中對這一問題進行了解決,增長了一個垃圾回收命令,刪除未被引用的層數據
打包上傳鏡像
1 [root@master scripts]# docker tag alpine:v1 192.168.0.153:5000/fbgweb:v1 2 [root@master scripts]# docker push 192.168.0.153:5000/fbgweb:v1 3 The push refers to a repository [192.168.0.153:5000/fbgweb] 4 e30c8796115a: Pushed 5 9922d8737f34: Pushed 6 c3cc0aba53da: Pushed 7 f73b42f6c905: Pushed 8 4a3fe911d00f: Pushed 9 187a385eda60: Pushed 10 0e23c00d0c30: Pushed 11 d221a7f5318b: Pushed 12 7e2d3752fd4f: Pushed 13 v1: digest: sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e size: 2204
查看倉庫鏡像 (查看倉庫鏡像腳本 get.py)
1 [root@master scripts]# python get.py 2 192.168.0.153:5000/fbgweb:v1 3 192.168.0.153:5000/nginx:1.7.9 4 192.168.0.153:5000/nginx:1.8
查看數據進行倉庫容器中,經過du命令查看大小
1 [root@master ~]# docker exec -it f70d0c79e6d546d4 sh 2 ~ # du -chs /var/lib/registry/ 3 182.4M /var/lib/registry/ 4 182.4M total
刪除鏡像
刪除鏡像對應的API以下:
DELETE /v2/<name>/manifests/<reference>
name:鏡像名稱
reference: 鏡像對應sha256值
發送請求,刪除剛纔上傳的鏡像
1 [root@master scripts]# curl -I -X DELETE http://192.168.0.153:5000/v2/fbgweb/manifests/sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e 2 HTTP/1.1 202 Accepted 3 Docker-Distribution-Api-Version: registry/2.0 4 X-Content-Type-Options: nosniff 5 Date: Mon, 13 Nov 2017 13:04:24 GMT 6 Content-Length: 0 7 Content-Type: text/plain; charset=utf-8
查看鏡像
1 [root@master scripts]# python get.py 2 192.168.0.153:5000/nginx:1.7.9 3 192.168.0.153:5000/nginx:1.8 4 192.168.0.153:5000/nginx:latest
能夠看到鏡像索引已經被刪除
查看數據大小
1 [root@master ~]# docker exec -it f70d0c79e6d546d4 sh 2 ~ # du -chs /var/lib/registry/ 3 182.4M /var/lib/registry/ 4 182.4M total
能夠看到數據大小沒有變化(只刪除了元數據)
垃圾回收
進行容器執行垃圾回收命令
1 ~ # registry garbage-collect /etc/docker/registry/config.yml 2 31 blobs marked, 5 blobs eligible for deletion 3 blob eligible for deletion: sha256:5e7cf06c8745d0985f94191c60aad8b87371c8a674162525bff0efccdb805931 4 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/5e/5e7cf06c8745d0985f94191c60aad8b87371c8a674162525bff0efccdb805931 go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0 5 blob eligible for deletion: sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e 6 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/6a/6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0 7 blob eligible for deletion: sha256:966e2fb5980b2a854c03551418af0b2ee2bef082dfaae075026d00fa36620960 8 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/96/966e2fb5980b2a854c03551418af0b2ee2bef082dfaae075026d00fa36620960 go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0 9 blob eligible for deletion: sha256:a122ad1ef7a033582abfc7bae980cc11bdcc2d19bed7d8ea5b7efc50d16456c8 10 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/a1/a122ad1ef7a033582abfc7bae980cc11bdcc2d19bed7d8ea5b7efc50d16456c8 go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0 11 blob eligible for deletion: sha256:b29205236f1d3eb6143e95f3a412a8f21a16a10b09c7aee28ecba3d803832285 12 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/b2/b29205236f1d3eb6143e95f3a412a8f21a16a10b09c7aee28ecba3d803832285 go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
查看數據大小
1 ~ # du -chs /var/lib/registry/ 2 159.5M /var/lib/registry/ 3 159.5M total
能夠看到鏡像數據已被刪除