1.进入registry容器
docker exec -it registry sh
2.开启删除功能
vi /etc/docker/registry/config.yml
version: 0.1
log:
fields:
service: registry
storage:
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
delete:
enabled: true
http:
addr: 5000
headers:
X-Content-Type-Options: [nosniff]
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
退出容器
3.查看垃圾文件&回收垃圾
docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml
打印出来的都是垃圾镜像
复制marking manifest sha256后面的内容,删除镜像
curl -I -X DELETE localhost:5000/v2/school-platform-server/manifests/sha256:fd4950ff8969d7e9265316bdd649464348b3a9abfb6251c8d0d7f6dd7d8ab886
localhost:5000/v2/school-platform-server相应的为容器名称
最后再执行垃圾回收命令
docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml
最后附上一个完整的python清理脚本
clear.py
import os, re
# execute command, and return the output
def execCmd(cmd):
r = os.popen(cmd)
text = r.read()
r.close()
return text
# write "data" to file-filename
def writeFile(filename, data):
f = open(filename, "w")
f.write(data)
f.close()
if __name__ == '__main__':
cmd = "docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml"
result = execCmd(cmd)
# 这里是容器名数组,根据自己的情况修改
containers = "school-external","school-auth"
for container in containers:
pattern = container+": marking manifest sha256:([\w]+)"
values = re.findall(pattern, result)
for value in values:
# localhost:5000是registry地址,根据自己的情况修改
cmd = "curl -I -X DELETE localhost:5000/v2/" + container + "/manifests/sha256:" + value
result = execCmd(cmd)
print(result)
execCmd("docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml")
执行脚本
python clear.py