清理docker-registry仓库

由于公司之前是使用docker-registry存储镜像的。时间久了仓库就满了。于是需要写个脚本清理一下仓库
参考registry官方提供的api说明。整理脚本如下

import sys, os, json, httplib, urllib, base64, socket, commands, urllib2
if len(sys.argv) < 2:
  print 'Please provide the repository name'
else:
  retained_num = 3;
  repsitory_domain = 'http://localhost:5000/v2'

  print 'Try to check ', sys.argv[1]
  image_name = sys.argv[1]
  print "\nBegin to list all the docker images..."
  print "Try to list images:  %s/%s/tags" % (repsitory_domain, image_name)
  request = urllib2.Request(url=repsitory_domain+'/'+image_name+'/tags/list')
  request.add_header('Authorization','Basic cnVuc2RhdGE6UnVuc2RhdGEjZG9ja2Vya')
  response = urllib2.urlopen(request)
  #print response.headers
  #status = response.headers['status']
  body = response.read().decode('utf-8')
  print body
  #print status
  if body == '':
    print "Error: empty body  " 
  else:
    item = json.loads(body)
    image_tags = item['tags']
    for tag in image_tags:
      print tag
    
      manifests_request = urllib2.Request(url=repsitory_domain+'/'+image_name+'/manifests/'+tag)
      manifests_request.add_header('Authorization','Basic cnVuc2RhdGE6UnVuc2RhdGEjZG9ja2Vyc')
      manifests_request.add_header('Accept','application/vnd.docker.distribution.manifest.v2+json')
      manifests_response = urllib2.urlopen(manifests_request)
      print type(manifests_response)
      etag = manifests_response.headers['Docker-Content-Digest']
      print etag
      
      print "Try to delete  %s/%s/manifests/%s" % (repsitory_domain, image_name, etag)
      delete_request = urllib2.Request(url=repsitory_domain+'/'+image_name+'/manifests/'+etag)
      delete_request.add_header('Authorization','Basic cnVuc2RhdGE6UnVuc2RhdGEjZG9ja2Vyd')
      delete_request.add_header('Accept','application/vnd.docker.distribution.manifest.v2+json')
      delete_request.get_method = lambda: 'DELETE'
      delete_response = urllib2.urlopen(delete_request)
      print delete_response.headers
      delete_result = manifests_response.headers['Content-Length']
      print delete_result

执行脚本以后发现空间并没有释放。因为registry的机制导致的。需要我们去手动的清理一下,然后勇安大佬丢给我连接说明了空间没有清理的原因。连接如下:
https://blog.csdn.net/rentian1/article/details/85130165

具体做法:

docker exec -it  59de6c752441 sh
cd /etc/docker/registry
registry garbage-collect config.yml  #等这句话执行完以后,空间就会释放了。
##注意 config.yaml 是挂载进去的。这是原始启动命令:docker run -d -p 5000:5000 --name $container_name  -v $basepath/config.yml:/etc/docker/registry/config.yml -v /home/data/registry-var/:/var/lib/registry/  registry:2.7.1

你可能感兴趣的:(技术研究)