Docker Restart All Containers

In this guide, we will see how to restart all the containers with an example.

Why Restart Containers? 

There are several reasons why you might need to restart a Docker container:

Application Updates: If you've just updated the application running inside a container, a restart can help apply those changes. 

Recovery: If containers face issues, a simple restart often brings them back to normal operation.

Configuration Updates: Post configuration changes, a restart ensures containers run with the latest settings.

Command to Restart Containers 

The primary command to restart a container is as straightforward as:


Restarting a Single Container: 

To restart a single container, simply specify its name or ID:

docker restart my_container_name


docker restart my_container_id

Restarting Multiple Containers: 

You can list down multiple container names or IDs:

docker restart my_container_1 my_container_2 my_container_3

Step-by-Step Example

1. Creating a Docker Image

For our example, we'll use a simple Node.js application, but the process applies to any application you'd want to dockerize.
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "server.js"]

2. Building the Image

With our Dockerfile ready, it's time to create an image:

docker build -t my-node-app:latest .

3. Starting the Container 

With our image built, let's start a container:

docker run -d -p 8080:8080 my-node-app:latest

4. Restarting All Containers 

After doing some changes in the node app in a container, you can restart the container by using the following command:

docker restart $(docker ps -a -q)

Here, docker ps -a -q lists all containers, and docker restart ensures they're all restarted.


In this guide, we have seen how to restart the Docker containers with examples.