1. Overview

In this tutorial, we’ll learn how to restart a single Docker container with Docker Compose.

2. Docker Compose restart Command

Docker Compose is a tool to manage multiple containers as a single service. However, the Docker Compose CLI includes commands that can apply to a single container. For example, the restart command lets us provide the name of the service we want to restart without affecting the other services that are running:

docker-compose restart service-name

Before diving into the restart command execution, let’s set up a working environment.

3. Setup

We must have a Docker container to run the Docker Compose commands. We’ll use a previous Baeldung project, spring-cloud-docker, which is a dockerized Spring Boot application. This project has two Docker containers that will help us prove that we can restart a single service without affecting the other.

First, we must confirm that we can run both containers by running the following command from the root of the project:

docker-compose up --detach --build

Now, we should be able to see both services running by executing docker-compose ps:

$ docker ps
     Name                   Command              State            Ports         
message-server   java -jar /message-server.jar   Up>8888/tcp
product-server   java -jar /product-server.jar   Up>9999/tcp

Additionally, we could go to localhost:18888 or localhost:19999 in our browser and verify that we see the messages displayed by the application services.

4. Restarting a Single Container

Up to this point, we have two containers running and managed as a single service by Docker Compose. Now, let’s see how we can use the restart command to stop and start only one of the two containers.

First, we’ll look at how we can achieve this without rebuilding the container. However, this solution will not update the service with the latest code. Then, we’ll see another approach where we build the container with the latest code before running.

4.1. Restarting Without Rebuilding

With both containers running, we pick one of the services to restart. In this case, we’ll use the message-server container:

docker-compose restart message-server

After we run the command in the terminal, we should be able to see the following message:

Restarting message-server ... done

Once the terminal prompts for another command, we can confirm that message-server has been restarted successfully by running the Docker command ps to check the states of all the running processes:

$ docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED          STATUS          PORTS                     NAMES
b6541d1c4ddf   product-server:latest   "java -jar /product-…"   10 minutes ago   Up 42 seconds>9999/tcp   product-server
1d07d2a7ed7d   message-server:latest   "java -jar /message-…"   10 minutes ago   Up 15 seconds>8888/tcp   message-server

Finally, we can determine that the command successfully restarted the message-server container by looking at the STATUS column. We can see how the message-server service has been up and running for a smaller period than the product-server service, which has been up since we ran the docker-compose up command in the previous section.

4.2. Rebuilding and Restarting

If a container needs to be updated with the latest code, running the restart command is not sufficient since the service needs to build first to pick up the code changes.

With both containers running, let’s change the code first to confirm we’ll update the service with the latest code before restarting. In the DockerProductController class, let’s change the return statement to look like this:

public String getMessage() {
    return "This is a brand new product";

Now, let’s build the Maven package:

mvn clean package

Now, we are ready to restart the product-server container. We can achieve this similarly to how we start the services, but this time by providing the name of the container we want to restart.

Let’s run the command to rebuild and restart the container:

docker-compose up --detach --build product-server

Now, we can verify that the product-server container restarted with the latest code by running docker ps and looking at the output:

$ docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED          STATUS          PORTS                     NAMES
78a4364e75e6   product-server:latest   "java -jar /product-…"   6 seconds ago    Up 5 seconds>9999/tcp   product-server
b559f742973b   message-server:latest   "java -jar /message-…"   22 minutes ago   Up 22 minutes>8888/tcp   message-server

As we can see, the product-server changed both the CREATED value and STATUS value, indicating that the service was rebuilt first and then restarted without any impact on the message-server.

Additionally, we can further confirm the code was updated by going to localhost:19999 in our browser and checking that the output is the latest.

5. Conclusion

In this article, we’ve learned how to restart a single container using Docker Compose. We covered two ways to achieve this.

First, we used the restart command with the name of the service to restart. Then, we made code changes to demonstrate we could rebuild the latest code and restart a single container without affecting the other.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.