How to Restart a Docker Container

David Mbochi Njonge Feb 02, 2024
  1. Pull an Nginx Image From the Docker Hub
  2. Run a Container From the Image
  3. Restart a Running Container
  4. Restart a Stopped Container
  5. Docker Container Exits Immediately
  6. Conclusion
How to Restart a Docker Container

When a container that was previously running in the background gets stopped, docker provides some commands that we can use to restart the container.

In this tutorial, we will learn how to restart a docker container with an Nginx server running in the background. We will also learn how some commands lead to the immediate termination of a container.

Pull an Nginx Image From the Docker Hub

To begin, we need to pull an Nginx image from the docker hub to run images from it. However, you can create your custom image using Dockerfile.

Open the terminal window on your computer and use the below command to pull the image.

~$ docker pull nginx:alpine

Run a Container From the Image

In this section, we will run a container with an Nginx server in the background. Use the below command to run a container with the name web-app.

~$ docker run --name web-app -d -p 8000:80 nginx:alpine

In this command, we have used the --name flag to create a name for the container. In addition, we have also used the flag -d, which allows us to run a container in the background.

Lastly, we have added the -p flag to publish the container’s port to the host. You can use the following command to verify that the container is up and running.

~$ docker ps

Output:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                  NAMES
c18fa4102fb2   nginx:alpine   "/docker-entrypoint.…"   5 seconds ago   Up 3 seconds   0.0.0.0:8000->80/tcp   web-app

Restart a Running Container

There are two approaches that we can use to restart a container. The first approach uses the docker start command while the second approach uses the docker restart command.

The docker start command is used to start a stopped container, and the docker restart command is used to restart a running container.

Since our container is already running, use the command below to restart the web-app container.

~$ docker restart web-app

Use the below command to verify that the container is up and running after restarting the container.

~$ docker ps

Output:

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS        PORTS                  NAMES
c18fa4102fb2   nginx:alpine   "/docker-entrypoint.…"   20 minutes ago   Up 1 second   0.0.0.0:8000->80/tcp   web-app

Restart a Stopped Container

This approach is the same as the previous one. We must use the docker stop command to stop the running container.

Once the container has been stopped, use the docker start command to restart the stopped container. Execute the following commands to see how this works in practice.

~$ docker stop web-app

Output:

web-app

If you run the docker ps -a command to list all the running and stopped containers, you can see that the web-app container’s status is exited, meaning the container has been stopped.

~$ docker ps -a

Output:

CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS                          PORTS      NAMES
c18fa4102fb2   nginx:alpine     "/docker-entrypoint.…"   32 minutes ago   Exited (0) About a minute ago              web-app

Finally, use the following command to restart the container.

~$ docker start web-app && docker ps

Output:

web-app
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                  PORTS                  NAMES
c18fa4102fb2   nginx:alpine   "/docker-entrypoint.…"   37 minutes ago   Up Less than a second   0.0.0.0:8000->80/tcp   web-app

Docker Container Exits Immediately

The container in the previous section did not terminate because the container had an Nginx process running in the background.

There are cases where the docker container immediately exits if it has finished executing its process. To illustrate this, run the below command to create a new container with the name temp-web-app.

~$ docker run -w /com/web-app/ --name temp-web-app -p 8080:80  nginx:alpine pwd

Output:

/com/web-app

Note that this command terminates immediately after printing the working directory /com/web-app. This is because the container exits once its main process executes the pwd command.

Therefore, commands like the one above should be used for testing purposes and not for running containers with a server, database, or application process. Use the below command to check the status of the temp-web-app container.

CONTAINER ID   IMAGE            COMMAND                  CREATED             STATUS                      PORTS                  NAMES
fb051673c15f   nginx:alpine     "/docker-entrypoint.…"   14 minutes ago      Exited (0) 3 minutes ago                           temp-web-app

Any attempt to restart the container will not work since the default command is set during the initial start-up. Meaning the container will always terminate after executing the pwd command where the container’s main process exits.

Conclusion

We learned in this tutorial how to restart a container in docker. We have covered how to start a running and stopping container.

We have learned the purpose of running containers that exit immediately with no processes running in the background.

David Mbochi Njonge avatar David Mbochi Njonge avatar

David is a back end developer with a major in computer science. He loves to solve problems using technology, learning new things, and making new friends. David is currently a technical writer who enjoys making hard concepts easier for other developers to understand and his work has been published on multiple sites.

LinkedIn GitHub

Related Article - Docker Container