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

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

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


当前回答

更新:从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

其他回答

从Windows shell中删除所有容器:

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

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

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-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系统修剪或docker容器修剪。请看VonC的最新答案。

以前的回答 综合上面几个不同的提示,删除所有非运行容器的最优雅的方法似乎是:

Docker rm $(Docker ps -q -f status=exit)

-q只打印容器id(没有列标题) -f允许你过滤打印的容器列表(在这种情况下,我们过滤只显示退出的容器)

https://github.com/HardySimpson/docker-cleanup

码头工人清理

一个小的一体化外壳,它删除:

一天前没有运行的容器 不属于任何剩余容器的映像

打算作为crontab作业运行

功能

它将删除所有<none>:<none>图像 如果映像有多个repo:标签引用,它将删除所有的repo:标签,除非运行一个容器。实际上这是“docker rmi”的本质。 许多错误信息将显示在屏幕上,你可以决定2>/dev/null或不 从docker-gc中学到一些东西,并修复了它的问题(它不能删除有多个repo:标签的图像)