📜  docker compose clean rebuild - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:40:48.963000             🧑  作者: Mango

Docker Compose Clean and Rebuild - Shell Bash

Introduction

In this guide, we will learn how to clean and rebuild docker-compose services using Shell Bash commands. Docker Compose is a tool that allows defining and running multi-container Docker applications. Sometimes, it becomes necessary to clean up the existing services and rebuild them from scratch. We will walk through the process of achieving this using the command line interface.

Prerequisites

Before getting started, ensure that the following prerequisites are met:

  • Docker Engine and Docker Compose are installed on the system.
  • Familiarity with basic Shell Bash commands and Docker Compose configurations.
Steps
Step 1: Stop and remove existing containers

Before rebuilding the services, we need to stop and remove any existing containers that are currently running. Use the following command to accomplish this:

docker-compose down

This command will stop and remove the containers defined in the docker-compose.yml file.

Step 2: Remove associated volumes and networks (optional)

If you want to completely clean up the environment, including any associated volumes and networks, use the following command:

docker-compose down --volumes --remove-orphans

This command will not only stop and remove the containers but also delete any unnamed volumes and networks that are not connected to any containers.

Step 3: Rebuild the services

After cleaning up the existing containers, you can move on to rebuilding the services defined in the docker-compose.yml file. The following command can be used for this purpose:

docker-compose up --build -d

This command will rebuild the defined services, pulling the latest images if necessary, and run them in detached mode (-d). The --build flag ensures that any changes made to the services or their Dockerfile(s) are applied.

Conclusion

In this guide, we have learned how to clean and rebuild docker-compose services using Shell Bash commands. By following the steps outlined above, you will be able to clean up any existing containers, remove associated volumes and networks if required, and rebuild the services from scratch using Docker Compose.