JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Restart the Docker container

Author:JIYIK Last Updated:2025/03/25 Views:

Docker provides some commands that we can use to restart the container when it is stopped while it was previously running in the background.

In this article, we will learn how to restart a docker container with an Nginx server running in the background. We will also see how some commands can cause the container to terminate immediately.


Pull the Nginx image from Docker Hub

First, we need to pull an Nginx image from docker hub to run the image. However, you can create a custom image using Dockerfile.

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

~$ docker pull nginx:alpine

Run a container from an image

In this section, we will run a container with Nginx server in the background. Use the following command to run a container named 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. Additionally, we have used the flag -d which allows us to run the container in the background.

Finally, we added the -p flag to publish the container’s ports to the host. You can verify that the container is up and running using the following command.

~$ 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

We can use two methods to restart the container. The first method uses docker startthe command, while the second method uses docker restartthe command.

docker startThe command is used to start a stopped container, and docker restartthe command is used to restart a running container.

Since our container is already running, restart the web-app container using the following command.

~$ docker restart web-app

After restarting the container, verify that the container is up and running using the following command.

~$ 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

Restarting a stopped container

This method is the same as the previous one. We have to docker stopstop the running container using the command.

Once the container is stopped, use docker startthe command to restart the stopped container. Execute the following command to see how it works in practice.

~$ docker stop web-app

Output:

web-app

If you run docker ps -athe command to list all running and stopped containers, you can see that the status of the web-app container is exited, indicating that the container has 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, restart the container using the following command.

~$ 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.

In some cases, if the docker container has completed the execution of its process, it will exit immediately. To illustrate this, run the following command to create a new container named temp-web-app.

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

Output:

/com/web-app

请注意, this command terminates immediately after printing the working directory /com/web-app. This is because the container exits as soon as 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 server, database, or application processes. Use the following 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

Since the default command is set during the initial startup, any attempt to restart the container will have no effect. This means that the container will always terminate after executing the pwd command when the container's main process exits.


Summarize

In this article, we have learned how to restart a container in docker. We have covered how to start a running and stopped container.

We have seen the purpose of running a container that exits immediately with no processes running in the background.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:122 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:141 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial