Docker

How to Clean up Docker Compose?

Docker is a project development and software-sharing technology widely used worldwide. It provides many built-in components used to manage its services, such as Docker Compose. Moreover, “docker-compose” is a command line utility for running the multi-container programs or services defined in the docker-compose file.

Occasionally, developers want to restart Docker compose from scratch and clean all its containers and volume.

This blog will demonstrate how to clean up Docker compose.

How to Clean up Docker Compose?

To clean up Docker compose to start it from scratch, follow the listed steps.

Step 1: Start Docker Compose

To create and start a container through Docker compose, utilize the “docker-compose up” command. Here, the “-d” option is used to execute the container in the container:

> docker-compose up -d

 

Step 2: Stop or Remove Docker Compose

To stop and remove the docker container created by “docker-compose”, use the “docker-compose down” command:

> docker-compose down

 

Alternatively, the “rm” command is also used to remove the Docker compose container as shown below:

> docker-compose rm

 

Step 3: Remove All Containers

In order to remove all containers, go through the provided command. Here, the “-f” option is used to remove containers forcefully, “-a” is used to remove all containers, and “-q” is used to display only the relevant containers id:

> docker rm -f $(docker ps -a -q)

 

Step 4: Remove Docker Volume

The Docker volume is the data and backup file that can be required by another container. To remove all Docker volumes, use the provided command:

> docker volume rm $(docker volume ls -q)

 

Step 5: Restart Docker Compose

Now, restart the Docker compose with the help of the provided command. This command will create and start the new container:

> docker-compose up -d

 

That was all about cleaning up the Docker Compose.

Conclusion

To clean up the Docker compose, first stop and remove the Docker container using the “docker-compose down” command. To remove the Docker compose container, utilize the “docker-compose rm” command. Next, remove the Docker volume to clean all data using the “docker volume rm $(docker volume ls -q)” command. Then, again start the Docker compose through the “docker-compose up” command. This write-up has demonstrated how to clean up Docker compose.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.