Docker

Explain the Difference Between Docker Compose Restart and Down

Docker Compose is a powerful tool utilized for specifying and executing numerous containers as a single service. Users can define the configuration for numerous Docker containers like images, volumes, ports, dependencies, etc., in one compose file using Docker Compose. It starts, restarts, and stops Docker containers together via the “docker-compose” command. The commonly utilized commands in Docker Compose are “up”, “restart”, and “down”.

This write-up will explain the following:

What is the Difference Between Docker Compose Restart and Down?

The “docker-compose restart” command that utilized to restart the running containers. It stops and starts Docker containers without removing them. It does not delete the container’s configuration, volumes, and network settings. This command is helpful when users want to apply changes to their service without losing any data.

In contrast, the “docker-compose down” command is used to stop and delete all the containers, images, volumes, and networks that were built by the “docker-compose up” command. It deletes all the data that was stored in the containers. This command is useful when the user wants to completely remove their service and free up resources on the system.

Example of Docker Compose Restart and Down

Let us take a scenario where we make a compose (YAML) file on Visual Studio Code and configure some services. Then, start, restart, and stop the compose services to find out how they work. Try out the below-provided instructions to do so.

Step 1: Create Compose File

First, create a “docker-compose.yml” file on Visual Studio Code and configure the desired services. For example, we have defined the below-mentioned services:

services:
  web:
    image: nginx:latest       //defining Docker image
    ports:
      - "9090:80"            //allocating port

In the above snippet:

  • services” key specifies the compose services. In our case, we have defined one service i.e., “web”.
  • web” is the name of the service.
  • image” specifies the Docker image for this service i.e., “nginx:latest”.
  • port” allocates the “9090:80” port.

Step 2: Start Compose Services

Then, start the compose services using the below-listed command:

docker-compose up -d

After executing the above command, the container has been built and started.

Step 3: View Running Containers

Ensure that the services have been started by viewing the running containers:

docker ps

The above output indicates that the compose services have been started and the running container can be seen.

Step 4: Restart Compose Services

To restart the compose services, type out the provided command:

docker-compose restart

Step 5: Verify Restarted Services

Display the running containers to ensure that the compose services have been started successfully:

docker ps

According to the above output, the compose services have been restarted.

Step 6: Stop Compose Services

In order to stop the compose services, run the below-given command:

docker-compose down

Step 7: Verification

Verify whether the services have been stopped or not using the given-provided command:

docker ps

As you can see the Docker container has been removed.

Conclusion

The “docker-compose restart” command stops and restarts Docker containers without removing them, their configuration, volumes, and network settings. In contrast, the “docker-compose down” command stops and  delete all the containers, images, volumes, and networks that were built by the “docker-compose up” command. This write-up explained the difference between Docker compose restart and down command with the practical implementation.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.