1. Overview
1.概述
A Docker container is an instance of a Docker image that runs some process inside it. As the state of this process changes, the behavior of the container is also affected. Thus, a container can be found in different states throughout its lifecycle.
Docker容器是一个Docker镜像的实例,在其内部运行一些进程。当这个进程的状态发生变化时,容器的行为也会受到影响。因此,一个容器在其整个生命周期中可以发现不同的状态。
In this tutorial, we’ll learn about all the possible states of a Docker container.
在本教程中,我们将了解Docker容器的所有可能状态。
Let’s first look into how to find the state of a Docker container, and then we’ll go through different stages of the container.
让我们首先了解一下如何找到Docker容器的状态,然后我们再来看看容器的不同阶段。
2. Find the Current State of a Docker Container
2.查找Docker容器的当前状态
Before we dive deep into different states of a Docker container, let’s first look into how to find the state of any Docker container.
在我们深入研究Docker容器的不同状态之前,首先让我们看看如何找到任何Docker容器的状态。
By default, the docker ps command displays the current state of all the Docker containers:
默认情况下,docker ps命令显示所有Docker容器的当前状态。
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f0b524f2d32 centos:7 "/bin/bash" 46 seconds ago Created strange_beaver
e6d798254d45 centos:7 "/bin/bash" About a minute ago Exited (0) About a minute ago wizardly_cohen
The output displays all the containers present on the machine with their STATUS (fifth column) and a bunch of other details.
输出显示机器上所有的容器,以及它们的STATUS(第五列)和其他一些细节。
We can also use the docker inspect command to get the status of a single container:
我们还可以使用docker inspect命令来获取单个容器的状态。
$ docker inspect -f '{{.State.Status}}' mycontainer
running
Here, mycontainer is the container name for which we wish to find the current state. We can also replace it with the Docker container id.
这里,mycontainer是我们希望找到当前状态的容器名称。我们也可以用Docker容器的ID来代替它。
3. Possible States of a Docker Container
3.Docker容器的可能状态
At any particular instance, a Docker container can be found in 6 possible states. Let’s now deep dive into each of these states:
在任何特定的实例中,Docker容器可以在6种可能的状态下被发现。现在让我们深入了解这些状态中的每一种。
3.1. Created
3.1.创建
Docker assigns the created state to the containers that were never started ever since they were created. Hence, no CPU or memory is used by the containers in this state.
Docker将created状态分配给那些自创建以来从未启动过的容器。因此,在这种状态下的容器不会使用CPU或内存。
The Docker containers created using the docker create command shows the status as created:
使用docker create命令创建的Docker容器显示状态为created:。
$ docker create --name mycontainer httpd
dd109e4be16219f1a6b9fc1cbfb050c1ae035d6a2c301ea0e93eb7d5252b8d2e
$ docker inspect -f '{{.State.Status}}' mycontainer
created
Here, we have created a Docker container, mycontainer, using the official Docker image of httpd. Since we have used the docker create command to launch the container, so the status is shown as created.
在这里,我们创建了一个Docker容器,mycontainer,使用httpd的官方Docker镜像。由于我们使用了docker create命令来启动这个容器,所以状态显示为created。
Such containers are useful when we need to keep ourselves prepared for some big tasks. In such situations, we create the container so that it’s ready to go when we start it.
当我们需要让自己为一些大的任务做好准备时,这样的容器很有用。在这种情况下,我们创建容器,以便当我们启动它时,它已经准备好了。
3.2. Running
3.2 运行
When we start a container having created a state using the docker start command, it attains the running state.
当我们使用e docker start命令启动一个创建状态的容器时,它就会达到运行的状态。
This state signifies that the processes are running in the isolated environment inside the container.
这种状态标志着进程正在容器内的隔离环境中运行。。
$ docker create --name mycontainer httpd
8d60cb560afc1397d6732672b2b4af16a08bf6289a5a0b6b5125c5635e8ee749
$ docker inspect -f '{{.State.Status}}' mycontainer
created
$ docker start mycontainer
mycontainer
$ docker inspect -f '{{.State.Status}}' mycontainer
running
In the above example, we first created a Docker container using the official httpd Docker image. Then, the status of the container was created. When we start the mycontainer, the httpd server process and all other relevant processes begin to run. Hence the status of the same container has now changed to running.
在上面的例子中,我们首先使用官方的httpd Docker镜像创建了一个Docker容器。然后,该容器的状态是创建的。当我们启动mycontainer时,httpd服务器进程和所有其他相关进程开始运行。因此,同一个容器的状态现在已经变成了运行中。
The container ran using the docker run command also attained the same status:
使用docker run命令运行的容器也达到了同样的状态。
$ docker run -itd --name mycontainer httpd
685efd4c1c4a658fd8a0d6ca66ee3cf88ab75a127b9b439026e91211d09712c7
$ docker inspect -f '{{.State.Status}}' mycontainer
running
In this state, there is no compromise in the CPU and memory consumption by the container.
在这种状态下,容器的CPU和内存消耗没有任何妥协。
3.3. Restarting
3.3.重新启动
Simply put, this state denotes that the container is under the process of restart.
简单地说,这种状态表示容器正在重启过程中。
Docker supports four types of restart policies, namely – no, on-failure, always, unless-stopped. Restart policy decides the behavior of the container when it exit.
Docker支持四种类型的重启策略,即 – no, on-failure, always, unless-stopped. 重启策略决定容器退出时的行为。
By default, the restart policy is set to no, which means that the container will not be started automatically after it exits.
默认情况下,重启策略被设置为no,这意味着容器退出后将不会自动启动。
Let’s update the restart policy to always and verify the Docker container state using the below example:
让我们把重启策略更新为always,并使用下面的例子验证Docker容器状态。
$ docker run -itd --restart=always --name mycontainer centos:7 sleep 5
f7d0e8becdac1ebf7aae25be2d02409f0f211fcc191aea000041d158f89be6f6
The above command will run the mycontainer and execute the sleep 5 command and exit. But since we have updated the restart policy for this container, it will automatically restart the container after it exits.
上述命令将运行mycontainer并执行sleep 5命令并退出。但由于我们已经为这个容器更新了重启策略,所以它将在退出后自动重启该容器。
After 5 seconds, the state of the container will be restarting:
5秒后,容器的状态将是重新启动。
$ docker inspect -f '{{.State.Status}}' mycontainer
restarting
3.4. Exited
3.4. 退出
This state is achieved when the process inside the container terminates. In this state, no CPU and memory are consumed by the container.
当容器内的进程终止时,就会达到这种状态。 在这种状态下,容器不会消耗任何 CPU 和内存。
There could be several reasons why a running container would exit. Let’s look into a few of them:
运行中的容器会退出可能有几个原因。让我们来看看其中的几个原因。
- The process inside the container was completed, and so it exited.
- The process inside the container encountered an exception while running.
- A container is intentionally stopped using the docker stop command.
- No interactive terminal was set to a container running bash.
$ docker run -itd --name mycontainer centos:7 sleep 10
596a10ddb635b83ad6bb9daffb12c1e2f230280fe26be18559c53c1dca6c755f
Here, we have started a centos container, mycontainer, and passed the command sleep 10. This will exit the container after 10 seconds of sleep. We can verify the same by running the following command after 10 seconds:
在这里,我们启动了一个 centos 容器,mycontainer,并通过命令sleep 10。这将在睡眠10秒后退出该容器。我们可以通过在10秒后运行以下命令来验证这一点。
$ docker inspect -f '{{.State.Status}}' mycontainer
exited
A container in the exited state cannot be accessed using the docker exec command. However, we can start the container using the docker start or docker restart and then access it.
处于exited状态的容器不能使用docker exec命令访问。然而,我们可以使用docker start或docker restart来启动该容器,然后访问它。
$ docker start mycontainer
3.5. Paused
3.5.暂停的
Paused is the state of a Docker container that suspends all the processes for an indefinite time.
暂停是Docker容器的状态,它将所有进程无限期地暂停。
A Docker container can be paused using the docker pause command.
可以使用docker pause命令暂停一个Docker容器。
$ docker run -itd --name mycontainer centos:7 sleep 1000
1a44702cea17eec42195b057588cf72825174db311a35374e250d3d1da9d70c5
In the above example, we started a Docker container using the centos Docker image and ran the command sleep 1000. This will exit the container after 1000 seconds of sleep.
在上面的例子中,我们使用centos Docker镜像启动了一个Docker容器,并运行sleep 1000命令。这将在睡眠1000秒后退出该容器。
Now let’s pause this container after a few seconds, say 100 seconds:
现在让我们在几秒钟后暂停这个容器,比如100秒。
$ docker pause mycontainer
mycontainer
$ docker inspect -f '{{.State.Status}}' mycontainer
paused
A paused container consumes the same memory used while running the container, but the CPU is released completely. Let’s verify this using the docker stat command:
一个暂停的容器所消耗的内存与运行容器时使用的相同,但CPU被完全释放。让我们使用docker stat命令来验证这一点。
$ docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
1a44702cea17 mycontainer 0.00% 1.09MiB / 7.281GiB 0.01% 1.37kB / 0B 0B / 0B 1
Notice that the CPU is 0%, but the memory usage is not zero.
请注意,CPU是0%,但内存使用量并不是零。
We can use the docker unpause command to resume the container:
我们可以使用docker unpause命令来恢复容器。
$ docker unpause mycontainer
mycontainer
On unpausing the container, it will resume from the same point where we paused it. In our example above, we paused the container after 100 seconds of sleep. So when we unpause the container, it will resume its sleep from 100. Therefore, the container will stop after 900 seconds of sleep from this point. (total sleep was set to 1000).
在取消容器的暂停时,它将从我们暂停它的同一点开始恢复。在我们上面的例子中,我们在睡眠了100秒后暂停了容器。因此,当我们取消对容器的暂停时,它将从100秒开始恢复睡眠。因此,容器将在从这一点开始的900秒睡眠后停止。(总睡眠被设置为1000)。
Now the question is when to pause a Docker container? Consider a situation when a Docker container is performing some CPU-intensive task. At the same time, we wish to run another CPU-intensive container with high priority. Of course, we can run both containers simultaneously, but it will slow down the execution due to a lack of resources.
现在的问题是什么时候暂停一个Docker容器?考虑一个情况,当一个Docker容器正在执行一些CPU密集型的任务。同时,我们希望以高优先级运行另一个CPU密集型的容器。当然,我们可以同时运行这两个容器,但由于资源不足,它的执行速度会减慢。
In such cases, we can pause one of the containers with low priority for some time and let the other container use the complete CPU. Once it’s done, we can unpause the execution of the first container.
在这种情况下,我们可以将其中一个低优先级的容器暂停一段时间,让另一个容器使用全部的CPU。一旦完成了,我们就可以解除对第一个容器的暂停执行。
3.6. Dead
3.6.死了
The dead state of a Docker container means that the container is non-functioning. This state is achieved when we try to remove the container, but it cannot be removed because some resources are still in use by an external process. Hence, the container is moved to the dead state.
Docker容器的dead状态意味着该容器是不运作的。这种状态是在我们试图移除容器时实现的,但由于一些资源仍在被外部进程使用,所以无法移除。因此,容器被转移到dead状态。
Containers in a dead state cannot be restarted. They can only be removed.
处于dead状态的容器不能被重新启动。它们只能被移除。
Since a container in a dead state is partially removed, so it does not consume any memory or CPU.
由于处于dead状态的容器被部分删除,所以它不消耗任何内存或CPU。
4. Conclusion
4.总结
In this tutorial, we went through different stages of a Docker container.
在本教程中,我们经历了Docker容器的不同阶段。
First, we looked into a couple of ways to find the state of a Docker container. Later, we learned the importance of each state and how to achieve those states using different Docker commands.
首先,我们研究了几种寻找Docker容器状态的方法。后来,我们了解了每种状态的重要性以及如何使用不同的Docker命令来实现这些状态。