这个问题与我是否应该关注过量的、未运行的Docker容器有关。
我想知道如何移除旧容器。docker rm 3e552code34a允许你删除一个,但我已经有很多了。Docker rm——help没有提供选择选项(像all一样,或者通过映像名称)。
也许有一个存放这些容器的目录,我可以很容易地手动删除它们?
这个问题与我是否应该关注过量的、未运行的Docker容器有关。
我想知道如何移除旧容器。docker rm 3e552code34a允许你删除一个,但我已经有很多了。Docker rm——help没有提供选择选项(像all一样,或者通过映像名称)。
也许有一个存放这些容器的目录,我可以很容易地手动删除它们?
当前回答
我想加上这个简单的答案,因为我没有看到它,这个问题是“老”而不是“全部”。
Sudo docker容器修剪-filter“until=24h”
根据您想要移除的容器的时间跨度调整24h。
其他回答
更新:从Docker 1.13版(2017年1月发布)开始,您可以发出以下命令来清理停止的容器、未使用的卷、悬空的映像和未使用的网络:
docker system prune
如果你想确保你只删除有退出状态的容器,使用这个:
docker ps -aq -f status=exited | xargs docker rm
类似地,如果你在清理docker的东西,你可以用这样的方式去除未标记的、未命名的图像:
docker images -q --no-trunc -f dangling=true | xargs docker rmi
删除所有docker: docker system prune -a
您可以使用存储库https://github.com/kartoza/docker-helpers中的docker-helper。安装后,只需输入drmc。
下面是我的docker-cleanup脚本,它删除未标记的容器和图像。请检查来源的任何更新。
#!/bin/sh
# 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.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {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 [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
来源:https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup
Bash的这两行代码会在删除容器之前通过一些关键字来过滤它们:
containers_to_keep=$(docker ps -a | grep 'keep\|Up\|registry:latest\|nexus' | awk '{ print $1 }')
containers_to_delete=$(docker ps -a | grep Exited | grep -Fv "$containers_to_keep" | awk '{ print $1 }')
docker rm $containers_to_delete
来自这篇文章。