这个问题与我是否应该关注过量的、未运行的Docker容器有关。
我想知道如何移除旧容器。docker rm 3e552code34a允许你删除一个,但我已经有很多了。Docker rm——help没有提供选择选项(像all一样,或者通过映像名称)。
也许有一个存放这些容器的目录,我可以很容易地手动删除它们?
这个问题与我是否应该关注过量的、未运行的Docker容器有关。
我想知道如何移除旧容器。docker rm 3e552code34a允许你删除一个,但我已经有很多了。Docker rm——help没有提供选择选项(像all一样,或者通过映像名称)。
也许有一个存放这些容器的目录,我可以很容易地手动删除它们?
当前回答
下面是我的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
其他回答
尝试使用此命令清除容器和悬空图像。
docker system prune -a
使用下面的命令来了解容器ID
docker ps
然后使用下面的命令来停止它。
docker kill <container-id>
docker rm --force `docker ps -qa`
在windows (powershell)上,你可以这样做:
$containers = docker ps -a -q Foreach ($container in $containers) {docker rm $container -f}
Use:
docker rm -f $(docker ps -a -q)
它强制停止并删除本地存在的所有容器。