Cleanup docker files: untagged containers and images.
#
Use docker-cleanup -n for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
# NOTE: "{12}" does not work with GNU Awk 3.1.7 (RHEL6).
# Ref: https://github.com/blueyed/dot ... 36470
docker ps -a | tail -n +2 | awk '$2 ~ "^+$" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
Dry-run.
if ; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
exit
fi
if ; then
echo "Cleanup docker files: remove untagged containers and images."
echo "Usage: ${0<span>*/} "<span>
echo " -n: dry run: display what would get removed."
exit 1
fi
7 个回复
tuxknight
赞同来自: 寻觅神迹
如果你能把 乱 用程序表达出来就可以做到批量删除。
test
赞同来自: cyan
docker rmi
docker images -f "dangling=true" -q
我用这句话删掉所有<none> 镜像
红字部分是用【`】撇号 括起来的
寻觅神迹 - 华为工程师。专注云计算和美食。
赞同来自:
技术上不要用强制删除~
但是这个事情不是仅靠技术能解决的~
追求
赞同来自:
希云cSphere(https://csphere.cn)的本地镜像管理就可以。可以根据各种条件筛选出想要删除的镜像,比如时间、名称等,然后选择全部,批量删除即可解决。
zenway33
赞同来自:
$ docker rm $(docker ps -a -q) 删除容器
$ docker rmi $(docker images -q) 删除镜像
请注意数据安全
allansun - 无名小虾
赞同来自:
<pre>
!/bin/sh
Cleanup docker files: untagged containers and images.
#Use
untagged_containers() {docker-cleanup -n
for a dry run to see what would be deleted.# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
# NOTE: "{12}" does not work with GNU Awk 3.1.7 (RHEL6).
# Ref: https://github.com/blueyed/dot ... 36470
docker ps -a | tail -n +2 | awk '$2 ~ "^+$" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
}
Dry-run.
if ; thenecho "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
if ; then
echo "Cleanup docker files: remove untagged containers and images."
echo "Usage: ${0<span>*/} "<span>
echo " -n: dry run: display what would get removed."
exit 1
fi
Remove containers with untagged images.
echo "Removing containers:" >&2untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
Remove untagged images
echo "Removing images:" >&2untagged_images 3 | xargs --no-run-if-empty docker rmi
</pre>
这是是我用了好久的代码了,可以把你说的乱七八糟没用的image都删掉
derekamz
赞同来自:
参考:
https://github.com/spotify/docker-gc