1. Overview

In this tutorial, we’ll learn how to restart a terminated or exited container. A Docker container can become stopped for several reasons. We’ll examine those and fix the problem based on the root cause in each case.

To show each scenario, we’ll use a sample Dockerfile. Also, we’ll learn how to restart an exited container using the docker start and docker restart commands.

2. Possible Reasons for Container Termination

Of course, a Docker container is just a process running in an isolated containerized environment. Thus, the ultimate reason for a Docker container to stop is that its main process gets terminated.

While there are others, these are the three typical Docker lifecycle states:

  • created
  • running
  • exited

The last one indicates that the main container process has completed.

Shutting down or terminating a Docker container doesn’t mean that there is a problem. For example, it can be its entry point script that causes it to stop.

2.1. Main Process Ended Successfully

When its main process completes its task, a container will eventually exit. To demonstrate, let’s create a sample Dockerfile:

FROM centos:7
CMD ["/bin/bash","-c","sleep 10"]

Above, we use centos:7 as the base image with a simple sleep command executed by bash as its main process. Next, let’s build the image using the docker build command:

$ docker build -t baeldung .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/2 : CMD ["/bin/bash","-c","sleep 10"]
 ---> Running in 1e89db0556d2
Removing intermediate container 1e89db0556d2
 ---> 40e0bfa75410
Successfully built 40e0bfa75410
Successfully tagged baeldung:latest

Now, we’ll run the container using docker run:

$ docker run -d --name baeldung baeldung
f5c27160a27596f9efdde3e70e221911da8bef4ebb0a214ff08dbc185cd29a0a

While running the container, we haven’t passed any command. Hence, the container will execute the sleep 10 command and terminate. This can be verified using the docker ps command. For the first 10 seconds after running the container, the docker ps command will show the baeldung container.

$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS                 NAMES
f5c27160a275   baeldung   "/bin/bash -c 'sleep…"       2 seconds ago    Created                                           baeldung

Now, if we run the docker ps command after 10 seconds, no container will be listed. Instead, the container has attained the exited state. This is so because the main process execution has been completed.

$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS     NAMES
f5c27160a275   baeldung   "/bin/bash -c 'sleep…"   2 minutes ago   Exited (0) 2 minutes ago                 baeldung

2.2. Main Process Terminated Prematurely

Another reason for container termination is the forceful stop of its main process. Let’s update the Dockerfile and add a long-running process:

FROM centos:7
CMD ["/bin/bash","-c","tail -f /dev/null"]

If we build the Docker image and then run a container using the above Dockerfile, we’ll discover that it won’t stop on its own. The reason is simple: tail -f /dev/null is an endless process. Therefore, this container will not stop until we force it.

2.3. Due to the docker stop Command

One of the simplest ways to terminate a Docker container is to stop it via the docker stop command:

$ docker stop baeldung
baeldung

Now, let’s check the container’s current status:

$ docker ps -a
CONTAINER ID   IMAGE   COMMAND                              CREATED          STATUS                     PORTS     NAMES
7b88ab4007af   centos  "/bin/bash -c 'tail -f /dev/null'"   33 seconds ago   Exited (0) 7 seconds ago             baeldung

Using the docker stop command, we can see that baeldung has stopped successfully.

2.4. Due to a Docker Service Restart

A Docker container can also go into the exited state if we reload the Docker service. In fact, a Docker service restart will stop all Docker containers.

Let’s take a look at this scenario by first running the baeldung container:

$ docker run -itd --name baeldung centos
7b88ab4007af29288000a13125bea05bc50308b95ff5ca07d17754a99b7a59b9

Now, if we restart the Docker service, the container should stop:

$ systemctl restart docker

Finally, we check the status of the container using the docker ps command:

$ docker ps -a
CONTAINER ID   IMAGE   COMMAND                              CREATED          STATUS                     PORTS     NAMES
7b88ab4007af   centos  "/bin/bash -c 'tail -f /dev/null'"   58 seconds ago   Exited (0) 7 seconds ago             baeldung

From the output above, we see that the baeldung is in the stopped state.

2.5. Container Consumes Too Much Memory

Linux may kill containers that consume a lot of memory or resources. To remedy this, we can increase the memory provided to a container. By default, Docker containers run with 256MB of memory.

3. Continue an Exited Docker Container

So far, we looked into the ways a container can reach the exited state. To reuse these terminated containers, we usually need to start them in one of two ways. Let’s examine both.

3.1. Restarting the Container

In this case, we just apply the regular commands to start or restart a container. Docker containers that stop abruptly can be addressed by this solution.

First, let’s see the command to restart a container:

$ docker restart baeldung

Of course, we can also use the docker start command to get the container back to a running state:

$ docker start baeldung

Both docker restart and docker start will simply move the container from the exited to the running state.

3.2. Using the Restart Policy

The Docker service is reloaded when we restart the host machine. Therefore, all running containers move to the exited state.

To avoid having to manually restart the containers with the methods above, we can instead use the –restart option with the docker run command. This option ensures that the Docker container is started automatically from the exited state.

Let’s run a Docker container with the –restart=always policy:

$ docker run -itd --restart=always --name baeldung centos
a267666232cc1c08ef438b04558ee163ab13cd965927b70ee79d3977336327b2

Now, if we restart the Docker service, the container should automatically start:

$ systemctl restart docker

Finally, let’s check the status of the container:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a267666232cc        centos              "/bin/bash"         9 seconds ago       Up 2 seconds                            baeldung

Using the restart policy, the Docker container will automatically restart as soon as it stops.

4. Conclusion

In this article, we learned how to run a terminated container in Docker. First, we looked into the possible causes for a Docker container to stop. Later, we resolved the issue by exploring ways to start and restart that container.


« 上一篇: 如何清理Linux系统
» 下一篇: 测试UDP端口连接