删除Docker私有仓库中的镜像

Linux大全评论1.5K views阅读模式

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相关教程见以下内容

Docker安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm

Ubuntu 14.04安装Docker  http://www.linuxidc.com/linux/2014-08/105656.htm

Ubuntu使用VNC运行基于Docker的桌面系统  http://www.linuxidc.com/Linux/2015-08/121170.htm

阿里云CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm

Ubuntu 15.04下安装Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm

在Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker http://www.linuxidc.com/Linux/2014-10/108184.htm

在 Ubuntu 15.04 上如何安装Docker及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm

Ubuntu 16.04上Docker使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm

企鹅博客
  • 本文由 发表于 2019年9月7日 05:45:45
  • 转载请务必保留本文链接:https://www.qieseo.com/168241.html

发表评论