Docker is very popular container technology. Docker is supported by Linux distributions and Windows operating systems. While using containers we may need to remove all container images from docker engine. We will look different ways to remove all docker container images in this tutorial.
Docker是非常流行的容器技术。 Linux发行版和Windows操作系统均支持Docker。 使用容器时,我们可能需要从Docker引擎中删除所有容器映像。 在本教程中,我们将寻找不同的方式来删除所有docker容器映像。
列出Docker映像 (List Docker Images)
Before removing images we generally need to list existing images to be sure. This is a safety step to prevent catastrophic actions like checking before dd
command. We will use sudo docker images
. We use sudo
because we need root
privileges or be in docker
user group.
在删除图像之前,我们通常需要列出现有图像以确保确定。 这是安全的步骤,可防止灾难性行为,例如在dd
命令之前进行检查。 我们将使用sudo docker images
。 我们使用sudo
是因为我们需要root
特权或位于docker
用户组中。
$ sudo docker images
![List Docker Images](https://i-blog.csdnimg.cn/blog_migrate/8154e51b46816b64dc56738786271e25.png)
删除所有容器(Delete All Containers)
Containers are instances created from the images. We can delete these instances or container images. We will use docker ps
command to list them and provide their UID to the docker rm
command which will delete given container.
容器是根据映像创建的实例。 我们可以删除这些实例或容器图像。 我们将使用docker ps
命令列出它们,并将其UID提供给docker rm
命令,该命令将删除给定的容器。
$ sudo docker rm $(sudo docker ps -a -q)
![Delete All Containers](https://i-blog.csdnimg.cn/blog_migrate/c7d259b70798d670740a9dff58370a15.png)
As we can see that deleted container UID is printed to the terminal output.
如我们所见,已删除的容器UID将打印到终端输出。
删除所有图像 (Delete All Images)
Images are pure data about the starting point of the containers. We generally download images from docker hub or create them and store for daily usage. If we do not need them we can delete then with docker rmi
command but we should provide images UID with the docker images
command like below.
图像是有关容器起点的纯数据。 我们通常从docker hub下载图像或创建图像并存储以供日常使用。 如果我们不需要它们,可以使用docker rmi
命令删除它,但是我们应该使用docker images
命令提供图像UID,如下所示。
$ sudo docker rmi -f $(sudo docker images -q)
![Delete All Images](https://i-blog.csdnimg.cn/blog_migrate/dab81a34514549dce0306c679db3e625.png)
强制删除具有附加卷的所有图像(Delete All Images Forcibly with Attached Volumes)
If there are some issues while deleting docker images and volumes we may need to force them for removal. We will use -f
option to for remove all docker images and containers. We will also use -v
option for verbosity in conjunction with -f
.
如果删除docker映像和卷时遇到一些问题,我们可能需要强制将其删除。 我们将使用-f
选项删除所有docker映像和容器。 我们还将-v
选项与-f
一起用于详细信息。
$ sudo docker rm -vf $(sudo docker ps -a -q)
删除所有未使用的图像和卷 (Delete All Unused Images and Volumes)
Docker engine is an intellygent mechanism where is monitors image and volume usage. Most of the cases there will be unused images and volumes where they take storage. We can use system prune
to remove unused images and volumes. We can also put this command into cron
to made it automatic.
Docker引擎是一种智能机制,可监视映像和卷使用情况。 大多数情况下,会有未使用的映像和卷存储在其中。 我们可以使用system prune
删除未使用的映像和卷。 我们还可以将此命令放入cron
以使其自动执行。
$ sudo docker system prune --all
![Delete All Unused Images and Volumes](https://i-blog.csdnimg.cn/blog_migrate/fcfb8a223560de75287cc606af01abea.png)