Docker uses images in order to start new container. New containers will use existing images and adds new layers for the customized parts. In this tutorial we will look how to manage docker images.
Docker使用映像来启动新容器。 新容器将使用现有图像,并为自定义零件添加新层。 在本教程中,我们将研究如何管理Docker映像。
列出Docker映像 (List Docker Images)
Before removing Docker Images we generally need to list these images. We will use list
command in order to list currently existing Docker Images.
在删除Docker映像之前,我们通常需要列出这些映像。 我们将使用list
命令来列出当前存在的Docker镜像。
$ sudo docker images
删除特定的Docker映像(Remove Specific Docker Image)
The first example to remove Docker image is removing single image. We need to specify the image with its name or image id. We will use rmi
command which is shortcut of remove image. In this example we will remove the image named ubuntu
.
删除Docker映像的第一个示例是删除单个映像。 我们需要使用名称或图像ID指定图像。 我们将使用rmi
命令,该命令是删除图像的快捷方式。 在此示例中,我们将删除名为ubuntu
的图像。
$ sudo docker rmi ubuntu
As we can see from output all layers related with ubuntu image is deleted. In the following example we will provide the image id.
从输出中可以看到,与ubuntu图像相关的所有图层均被删除。 在以下示例中,我们将提供图像ID。
$ sudo docker rmi 3bee3060bfc8
列出悬空的Docker映像(List Dangling Docker Images)
As we know docker images consist of different layers. These layers are used by multiple or single images. Sometimes layers can be unrelated with any image. We can list these unused and unrelated docker image layers with -f
and dangling=true
options.
众所周知,docker映像由不同的层组成。 这些层由多个或单个图像使用。 有时,图层可能与任何图像都不相关。 我们可以使用-f
和dangling=true
选项列出这些未使用且不相关的docker图像层。
$ docker images -f dangling=true
删除悬空的Docker映像 (Remove Dangling Docker Images)
We can remove dangling images by one by one typing the docker image id but this is not a feasible solution. We can list the dangling docker images id’s with -q
parameter .
我们可以通过键入docker image id来删除悬空的映像,但这不是一个可行的解决方案。 我们可以使用-q
参数列出悬空的docker映像ID。
$ docker rmi $(docker images -f dangling=true -q)
列出具有指定名称和正则表达式的Docker映像 (List Docker Images With Specified Name and Regex)
In enterprise environments there will be a lot of images we need to cope. We can use pattern search or regex with grep
tool to filter and list specific named images. In this example we will list images those contains buntu
in their names.
在企业环境中,我们需要处理很多图像。 我们可以使用模式搜索或带有grep
工具的正则表达式来过滤和列出特定的命名图像。 在此示例中,我们将列出名称中包含buntu
图像。
$ sudo docker images | grep "buntu"
删除具有指定名称和正则表达式的Docker映像(Remove Docker Images With Specified Name and Regex)
We can use rmi
to remove docker images those have specified names or patterns. In this example we will remove images those names have buntu
.
我们可以使用rmi
删除具有指定名称或模式的docker映像。 在此示例中,我们将删除名称为buntu
图像。
$ docker images | grep "buntu" | awk '{print $1}' | xargs docker rmi
列出所有Docker映像 (List All Docker Images)
We can use -a
option to list all docker images.
我们可以使用-a
选项列出所有docker镜像。
$ docker images -a
删除所有Docker映像(Remove All Docker Images)
We can also remove all docker images just using similar technique like previous examples. We will provide the image id’s to the rmi
command.
我们还可以使用与先前示例类似的技术删除所有docker映像。 我们将为rmi
命令提供图像ID。
$ docker rmi $(docker images -a -q)