centos7 Docker私有倉庫搭建及刪除鏡像

搭建私有倉庫

要獲取全部容器名稱及其IP地址只需一個命令。

docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)


在109機器上下載registry鏡像css

docker pull registry

下載完以後咱們經過該鏡像啓動一個容器python

docker run -d -p 5000:5000 registry

默認狀況下,會將倉庫存放於容器內的/tmp/registry目錄下,這樣若是容器被刪除,則存放於容器中的鏡像也會丟失,因此咱們通常狀況下會指定本地一個目錄掛載到容器內的/tmp/registry下,nginx

不過具體的狀況仍是要到容器裏去看web

先啓動容器docker

docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry 
b4c21ca8cf8a23ea72e0471909742541ffc312ea5cf492486b5bdc3130179864

 

能夠看到容器存放位置不在/tmp 下express

咱們接着來查找下,掛載位置到底在哪裏json

能夠看到registry 掛載目錄是 在 /var/lib/registry 下vim

咱們從新啓動下 registrycentos

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 這個是什麼呢?咱們在下面刪除倉庫鏡像介紹服務器

這裏須要說明一點,在啓動倉庫時,需在配置文件中的storage配置中增長delete=true配置項,容許刪除鏡像。默認的鏡像是沒有這個參數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cat config.yml
 
version:  0.1
log:
   fields:
     service: registry
storage:
   delete:
     enabled: true
   cache:
     blobdescriptor: inmemory
   filesystem:
     rootdirectory: /var/lib/registry
http:
   addr: : 5000
   headers:
     X-Content-Type-Options: [nosniff]
health:
   storagedriver:
     enabled: true
     interval:  10 s
     threshold:  3

  

能夠看到咱們啓動了一個容器,地址爲:192.168.0.109:5000。

 

測試

接下來咱們就要操做把一個本地鏡像push到私有倉庫中。首先在109機器下pull一個比較小的鏡像來測試(此處使用的是busybox)。

docker pull centos

接下來修改一下該鏡像的tag。

docker tag busybox 192.168.0.109:5000/centos

接下來把打了tag的鏡像上傳到私有倉庫。

docker push 192.168.0.109:5000/centos

能夠看到 push 失敗:

 Error: Invalid registry endpoint https://192.168.0.109:5000/v1/: Get https://192.168.0.109:5000/v1/_ping: dial tcp 192.168.0.109: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.109:5000/ca.crt 

由於Docker從1.3.X以後,與docker registry交互默認使用的是https,然而此處搭建的私有倉庫只提供http服務,因此當與私有倉庫交互時就會報上面的錯誤。爲了解決這個問題須要在啓動docker server時增長啓動參數爲默認使用http訪問。修改docker啓動配置文件:

vim  /usr/lib/systemd/system/docker.service 

找到 ExecStart

ExecStart=... --insecure-registry 192.168.0.109:5000 \ 

重啓docker:

systemctl daemon-reload
systemctl restart docker

重啓完以後咱們再次運行推送命令,把本地鏡像推送到私有服務器上。

docker push 192.168.0.109:5000/centos  

 

接下來咱們從私有倉庫中pull下來該鏡像。

sudo docker pull 192.168.0.109:5000/centos

查看鏡像

# curl -XGET http://registry:5000/v2/_catalog
# curl -XGET http://registry:5000/v2/image_name/tags/list

 

Registry刪除鏡像、垃圾回收

Docker倉庫在2.1版本中支持了刪除鏡像的API,但這個刪除操做只會刪除鏡像元數據,不會刪除層數據。在2.4版本中對這一問題進行了解決,增長了一個垃圾回收命令,刪除未被引用的層數據

打包上傳鏡像

複製代碼
[root@master scripts]# docker tag alpine:v1 192.168.0.109:5000/fbgweb:v1
[root@master scripts]# docker push 192.168.0.109:5000/fbgweb:v1
The push refers to a repository [192.168.0.109:5000/fbgweb]
e30c8796115a: Pushed 
9922d8737f34: Pushed 
c3cc0aba53da: Pushed 
f73b42f6c905: Pushed 
4a3fe911d00f: Pushed 
187a385eda60: Pushed 
0e23c00d0c30: Pushed 
d221a7f5318b: Pushed 
7e2d3752fd4f: Pushed 
v1: digest: sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e size: 2204
複製代碼

 

查看倉庫鏡像 (get.py須要安裝模塊)

pip 安裝:

複製代碼
首先安裝epel擴展源:
  yum -y install epel-release
  更新完成以後,就可安裝pip:
  yum -y install python-pip
  安裝完成以後清除cache:
  yum clean all
複製代碼

request模塊安裝:

pip install request
複製代碼
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import requests  
import json  
import traceback  
   
repo_ip = '192.168.0.109'  
repo_port = 5000  
   
def getImagesNames(repo_ip,repo_port):  
    docker_images = []  
    try:  
        url = "http://" + repo_ip + ":" +str(repo_port) + "/v2/_catalog"  
        res =requests.get(url).content.strip()  
        res_dic = json.loads(res)  
        images_type = res_dic['repositories']  
        for i in images_type:  
            url2 = "http://" + repo_ip + ":" +str(repo_port) +"/v2/" + str(i) + "/tags/list"  
            res2 =requests.get(url2).content.strip()  
            res_dic2 = json.loads(res2)  
            name = res_dic2['name']  
            tags = res_dic2['tags']  
            for tag in tags:  
                docker_name = str(repo_ip) + ":" + str(repo_port) + "/" + name + ":" + tag  
                docker_images.append(docker_name)  
                print docker_name  
    except:  
        traceback.print_exc()  
    return docker_images  
   
a=getImagesNames(repo_ip, repo_port)  
#print a  
[root@master scripts]# python get.py                           
192.168.0.109:5000/fbgweb:v1
192.168.0.109:5000/nginx:1.7.9
192.168.0.109:5000/nginx:1.8

查看數據進行倉庫容器中,經過du命令查看大小

[root@master ~]# docker exec -it f70d0c79e6d546d4 sh
~ # du  -chs  /var/lib/registry/
182.4M  /var/lib/registry/
182.4M  total

刪除鏡像

刪除鏡像對應的API以下:

DELETE /v2/<name>/manifests/<reference>

name:鏡像名稱

reference: 鏡像對應sha256值

 

發送請求,刪除剛纔上傳的鏡像

複製代碼
[root@master scripts]#  curl -I -X DELETE http://192.168.0.109:5000/v2/fbgweb/manifests/sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Mon, 13 Nov 2017 13:04:24 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8
複製代碼

 

查看鏡像

[root@master scripts]# python get.py 
192.168.0.109:5000/nginx:1.7.9
192.168.0.109:5000/nginx:1.8
192.168.0.109:5000/nginx:latest

能夠看到鏡像索引已經被刪除

查看數據大小

[root@master ~]# docker exec -it f70d0c79e6d546d4 sh
~ # du  -chs  /var/lib/registry/
182.4M  /var/lib/registry/
182.4M  total

能夠看到數據大小沒有變化(只刪除了元數據)

垃圾回收

進行容器執行垃圾回收命令

複製代碼
~ # registry garbage-collect /etc/docker/registry/config.yml 
31 blobs marked, 5 blobs eligible for deletion
blob eligible for deletion: sha256:5e7cf06c8745d0985f94191c60aad8b87371c8a674162525bff0efccdb805931
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/5e/5e7cf06c8745d0985f94191c60aad8b87371c8a674162525bff0efccdb805931  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/6a/6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:966e2fb5980b2a854c03551418af0b2ee2bef082dfaae075026d00fa36620960
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/96/966e2fb5980b2a854c03551418af0b2ee2bef082dfaae075026d00fa36620960  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:a122ad1ef7a033582abfc7bae980cc11bdcc2d19bed7d8ea5b7efc50d16456c8
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/a1/a122ad1ef7a033582abfc7bae980cc11bdcc2d19bed7d8ea5b7efc50d16456c8  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:b29205236f1d3eb6143e95f3a412a8f21a16a10b09c7aee28ecba3d803832285
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/b2/b29205236f1d3eb6143e95f3a412a8f21a16a10b09c7aee28ecba3d803832285  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
複製代碼

查看數據大小

~ # du  -chs  /var/lib/registry/
159.5M  /var/lib/registry/
159.5M  total

能夠看到鏡像數據已被刪除

相關文章
相關標籤/搜索