1. Overview
1.概述
In a previous article, we learned how to remove a Docker image. However, a Docker image can only be removed if no Docker container is using that image. Hence, to remove a Docker image, it is necessary to remove all the Docker containers running with that image.
在之前的文章中,我们学习了如何删除Docker镜像。然而,只有在没有Docker容器使用该镜像的情况下才能删除Docker镜像。因此,要删除一个Docker镜像,就必须删除使用该镜像运行的所有Docker容器。
In this tutorial, we’ll learn to remove Docker containers using different approaches.
在本教程中,我们将学习使用不同的方法来移除Docker容器。
2. Why Remove a Docker Container?
2.为什么要删除一个Docker容器?
When a Docker container completes its execution, it attains the exited state. Such containers do not consume any CPU or memory, but they still use the machine’s disk space. Also, stopped containers are not automatically removed unless we use the –rm flag while running the Docker container.
当一个Docker容器完成其执行时,它就达到了exited状态。这种容器不消耗任何CPU或内存,但它们仍然使用机器的磁盘空间。此外,除非我们在运行Docker容器时使用-rm标志,否则停止的容器不会被自动删除。
Hence, as more and more containers are moved into the exited state, the overall disk space consumed by them increases. As a result, we might not be able to launch new containers, or the Docker daemon would stop responding.
因此,随着越来越多的容器被移到exited状态,它们所消耗的总体磁盘空间也会增加。因此,我们可能无法启动新的容器,或者Docker守护程序会停止响应。
To avoid such scenarios, it is recommended to either run the Docker containers using the –rm flag or periodically remove the Docker containers manually.
为了避免这种情况,建议使用-rm标志运行Docker容器,或者定期手动删除Docker容器。
Let’s now learn how to remove Docker containers.
现在我们来学习如何删除Docker容器。
3. Remove a Single Docker Container
3.移除单个Docker容器
First, we’ll start a CentOS Docker container in non-interactive mode. By doing so, the container will stop immediately after we run the container:
首先,我们将以非交互式模式启动一个CentOS Docker容器。通过这样做,在我们运行容器后,容器将立即停止。
$ docker run --name mycontainer centos:7
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
418c28b4b04e centos:7 "/bin/bash" 6 seconds ago Exited (0) 5 seconds ago mycontainer
Now let’s remove the Docker container, mycontainer, using the docker rm command:
现在让我们使用docker rm命令删除Docker容器,mycontainer。
$ docker rm mycontainer
mycontainer
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
We can also use Docker container id instead of the Docker container name to remove the Docker container using the docker rm command:
我们也可以用Docker容器ID代替Docker容器名称,使用docker rm命令删除Docker容器。
$ docker rm 418c28b4b04e
4. Remove Multiple Docker Containers
4.移除多个Docker容器
We can also remove multiple Docker containers using the docker rm command. The docker rm command accepts a space-separated list of the Docker container name or id and removes all of them:
我们还可以使用docker rm命令删除多个Docker容器。docker rm命令接受一个以空格分隔的Docker容器名称或ID的列表,并删除所有这些容器。
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23c70ec6e724 centos:7 "/bin/bash" 6 seconds ago Exited (0) 5 seconds ago mycontainer3
fd0886458666 centos:7 "/bin/bash" 10 seconds ago Exited (0) 9 seconds ago mycontainer2
c223ec695e2d centos:7 "/bin/bash" 14 seconds ago Exited (0) 12 seconds ago mycontainer1
$ docker rm c223ec695e2d mycontainer2 23c70ec6e724
c223ec695e2d
mycontainer2
23c70ec6e724
In the example above, there were three Docker containers in the exited state which we removed using the docker rm command.
在上面的例子中,有三个Docker容器处于exited状态,我们使用docker rm命令将其移除。
We can use the Docker container name and id interchangeably with any Docker command. Notice that we used the Docker container id for the mycontainer1 and mycontainer3 and the container name for mycontainer2.
我们可以在任何Docker命令中交替使用Docker容器名称和ID。注意,我们对mycontainer1和mycontainer3使用了Docker容器id,对mycontainer2使用了容器名称。
5. Remove All the Docker Containers
5.删除所有的Docker容器
Consider a scenario where too many stopped Docker containers are present on the machine, and now we wish to remove them all. Of course, we can use the above approach and pass all the containers id to the docker rm command. But let’s look into a more optimized and simple command to remove all the Docker containers:
考虑这样一个场景:机器上有太多停止的Docker容器,现在我们希望将它们全部删除。当然,我们可以使用上述方法,将所有的容器ID传递给docker rm命令。但是,让我们来看看一个更加优化和简单的命令来删除所有的Docker容器。
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5c45fa5764f centos:7 "/bin/bash" 4 seconds ago Exited (0) 3 seconds ago mycontainer1
ed806b1743cd centos:7 "/bin/bash" 9 seconds ago Exited (0) 7 seconds ago mycontainer2
2e00a052eb12 centos:7 "/bin/bash" 13 seconds ago Exited (0) 12 seconds ago mycontainer3
$ docker rm $(docker ps -qa)
b5c45fa5764f
ed806b1743cd
2e00a052eb12
The command docker ps -qa return the numeric ids of all the containers present on the machine. All these ids are then passed to the docker rm command, which will iteratively remove the Docker containers.
命令docker ps -qa返回机器上存在的所有容器的数字ID。所有这些ID将被传递给docker rm命令,它将迭代地移除Docker容器。
We can also use the docker container prune command to remove all the stopped containers:
我们还可以使用docker container prune命令来删除所有停止的容器。
$ docker container prune -f
Here, we use the -f flag to avoid the prompt for confirmation.
在这里,我们使用-f标志来避免提示确认。
6. Forcefully Remove a Running Docker Container
6.强行删除正在运行的Docker容器
All the commands that we discussed in the examples above works only if the Docker container is stopped. If we try to remove a running container without stopping it first, we’ll get an error message similar to this:
我们在上面的例子中讨论的所有命令只有在Docker容器停止后才能工作。如果我们试图删除一个正在运行的容器而不先停止它,我们会得到一个类似这样的错误信息。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f84692b27b0a centos:7 "/bin/bash" 59 seconds ago Up 58 seconds mycontainer
$ docker rm mycontainer
Error response from daemon:
You cannot remove a running container f84692b27b0a18266f34b35c90dad655faa10bb0d9c85d73b22079dde506b8b5.
Stop the container before attempting removal or force remove
One way to remove a running Docker container is first to stop that container using the docker stop command and then use the docker rm command to remove it.
删除一个正在运行的Docker容器的方法是首先使用docker stop命令停止该容器,然后使用docker rm命令来删除它。
Another way is to forcefully remove such containers using the -f option:
另一种方法是使用-f选项强行删除此类容器。
$ docker rm -f mycontainer
mycontainer
We can use the -f option to remove a single Docker container, multiple Docker containers, or all the Docker containers.
我们可以使用-f选项来删除单个Docker容器、多个Docker容器或所有的Docker容器。
7. Conclusion
7.结语
In this tutorial, we gained an understanding of why it is necessary to remove Docker containers. First, we learned to remove a container from a Linux machine. Further, we bulk removed the Docker containers using the docker rm and docker prune command.
在本教程中,我们了解了为什么有必要删除Docker容器。首先,我们学会了从Linux机器上删除一个容器。此外,我们使用docker rm和docker prune命令批量删除Docker容器。
Finally, we looked at how to remove Docker containers forcefully, which are in the running state.
最后,我们研究了如何强行删除处于运行状态的Docker容器。