我在OS X 10.8.4(Mountain Lion)下运行Vagrant下的Docker,每当我尝试删除保存的图像时,都会出现错误:

$ docker rmi some-image-id
2013/07/15 hh:mm:ss unexpected JSON input

根据rmi帮助,正确的语法是docker rmi IMAGE[IMAGE…],我不知道该怎么做。

如何删除图像?

$ docker version
Client version: 0.4.8
Server version: 0.4.8
Go version: go1.1

 

$docker info
Containers: 1
Images: 3

有趣的是,当我运行docker ps时,根本没有容器出现。运行docker图像显示四(4)个基本图像和一(1)个节点图像。


当前回答

更新,正如VonC在如何删除旧Docker容器中所评论的。

使用Docker 1.13(2016年第4季度),您现在可以:

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

请参阅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

其他回答

Docker提供了一些删除图像的命令。

显示/删除图像:

docker images
docker images -a # All images
docker images --no-trunc # List the full length image IDs

docker images --filter "dangling=true" // Show unstage images
docker rmi $(docker images -f "dangling=true" -q) # Remove on unstages images

docker rmi <REPOSITORY> or <Image ID> # Remove a single image

docker image prune # Interactively remove dangling images
docker image prune -a # Remove all images

or 

docker rmi -f $(sudo docker images -a -q)

此外,您还可以使用过滤器参数删除一次:

例子:

$docker images --filter "before=<hello-world>" // It will all images before hello-world

$docker images --filter "since=<hello-world>" // It will all images since hello-world

因此,您可以像这样删除过滤器图像:

docker rmi $(docker images --filter "since=<hello-world>")
docker rmi $(docker images --filter "before=<hello-world>")

下面是一个shell脚本,用于删除标记(命名)的图像及其容器。另存为docker rmi并使用“docker rmi-my image name”运行

#!/bin/bash

IMAGE=$1

if [ "$IMAGE" == "" ] ; then
  echo "Missing image argument"
  exit 2
fi

docker ps -qa -f "ancestor=$IMAGE" | xargs docker rm
docker rmi $IMAGE

docker rm容器名称

docker rmi映像名称

码头工人帮助rm删除一个或多个容器rmi删除一个或多个图像

尝试docker rmi节点。这应该奏效。

看到所有创建的容器就像docker ps-a一样简单。

要删除所有现有容器(而不是图像!),请运行docker rm$(docker ps-aq)

首先,我们必须停止并删除Docker容器,这些容器与我们将要删除的Docker映像相关联。

因此,首先

docker stop container id-停止正在运行的容器docker rm container id-删除/删除容器

然后

docker rmi image id-删除/删除图像