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

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial