这个问题与我是否应该关注过量的、未运行的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 rm  $(docker ps -a | awk '/myimage:mytag/{print $1}')

另一种方法,我从Guillaume J. Charmes那里得到的(出处请注明出处)

docker rm `docker ps --no-trunc -aq`

将以优雅的方式移除所有容器。

由Bartosz Bilicki为Windows编写:

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

美国小妞PowerShell:

docker rm @(docker ps -aq)

Docker 1.13(2016年第四季度)的更新,归功于VonC(稍后在本帖子中):

docker系统修剪将删除所有未使用的数据(即,按顺序:容器停止,卷没有容器和图像没有容器)。

请参阅PR 26108和commit 86de7c0,它们引入了一些新命令,以帮助方便地可视化Docker守护进程数据在磁盘上占用了多少空间,并允许轻松清理“不需要的”多余空间。

docker system prune

WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all images without at least one container associated to them
Are you sure you want to continue? [y/N] y

从Windows shell中删除所有容器:

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

下面是一个脚本,用于删除创建时间超过2天的正在运行和已退出的容器:

#!/bin/bash
# This script will kill and remove containers older than 2 days.
#
docker ps -aq > /scripts/containers.txt
today=`date +%Y-%m-%d`
oldate=`date --date="2 day ago" +%Y-%m-%d`
while read p; do
    cont=`docker inspect -f '{{ .Created }}' $p | cut -c 1-10`
    echo " Created date of $p is $cont"
    k=`echo $(( ( $(date -ud $today +'%s') - $(date -ud $cont +'%s'))/60/60/24 ))`
    echo $k
    if [ $k -ge 2 ];
    then
            echo "Killing docker container $p"
            docker kill $p
            echo "Removing docker container $p"
            docker rm $p
    else
            echo "Docker container $p is not one day old, so keeping the container."
    fi
done </scripts/containers.txt

更新2021年(最新)

docker container prune

这是2017年(旧)的方式

移除所有停止的容器

docker rm $(docker ps -a -q)

移除所有容器(已停止和未停止)

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