Docker私有仓库v2版本中的镜像,官方不建议删除,但是也提供了删除接口:
DELETE /v2/<name>/manifests/<reference>
Host: <registry host>
Authorization: <scheme> <token>
删除的原理就是把索引删掉,但磁盘上的数据是删不掉的。这是由于各个镜像之间的不同层共用的关系,可能导致删除一个镜像后其余的镜像也无法使用了。
用Python实现伪删除,代码如下:
#-*- coding:utf-8 -*-
#!/usr/bin/env python
'''
Created on 2016.10.8
@desc: delete images from repo
'''
import requests
repo_ip = "172.16.4.254"
repo_port = 5000
image_name = "oa"
image_tag = "20161012"
def deleteImagesFromRepo(repo_ip,repo_port,image_name,image_tag):
head = {'Accept': 'application/vnd.docker.distribution.manifest.v2+json'}
reg = "http://" + repo_ip + ":" + str(repo_port) + "/v2/"
res = requests.get(reg + image_name + "/manifests/" + image_tag,headers=head,verify=False)
manifest = res.headers['Docker-Content-Digest']
res2 = requests.delete(reg + image_name + "/manifests/"+manifest,verify=False)
if res2.status_code == 202:
print "delete %s success......"%(image_name+':'+image_tag)
else:
print "delete %s fail......"%(image_name+':'+image_tag)
deleteImagesFromRepo(repo_ip,repo_port,image_name,image_tag)
运行结果如下:
更多Docker相关教程见以下内容:
Docker安装应用(CentOS 6.5_x64)
Ubuntu 14.04安装Docker
在Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker