这个问题与我是否应该关注过量的、未运行的Docker容器有关。

我想知道如何移除旧容器。docker rm 3e552code34a允许你删除一个,但我已经有很多了。Docker rm——help没有提供选择选项(像all一样,或者通过映像名称)。

也许有一个存放这些容器的目录,我可以很容易地手动删除它们?


当前回答

您可以使用存储库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

Use:

docker rm -f $(docker ps -a -q)

它强制停止并删除本地存在的所有容器。

移除所有停止使用的容器:

docker rm $(docker ps -a | grep Exited | awk '{print $1}')

来自pauk960的评论:

从1.3.0版本开始,你可以使用docker ps过滤器,而不是grep Exited使用docker ps -a -f status= Exited。如果你使用-q,你只能得到容器id,而不是完整的输出,不需要使用awk。

我用的是:

docker rm -v $(docker ps -a -q -f status=exited)

删除已退出的容器:

docker rmi -f $(docker images | grep "<none>" | awk "{print \$3}")

为了摆脱所有未标记的图像。

删除所有docker: docker system prune -a