diff --git a/Containerization & Orchestration/Docker/4-Docker-Compose.md b/Containerization & Orchestration/Docker/4-Docker-Compose.md index 6b07359..09ebfff 100644 --- a/Containerization & Orchestration/Docker/4-Docker-Compose.md +++ b/Containerization & Orchestration/Docker/4-Docker-Compose.md @@ -1,2 +1,83 @@ # **Docker Compose Guide** +Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to manage services, networks, and volumes using a YAML configuration file. + +--- + +## **1. Basic Docker Compose Structure** + +Before defining the services in Docker Compose, we need to specify the Docker Compose version and the services we want to run. Here's a basic YAML template: + +```yaml +version: "" + +services: + : # The name of your service (e.g., web, db) + container_name: # The name of the container + image: # Docker image to be used for this service + restart: always # Ensure the container restarts if it stops or fails + ports: + - ":" # Map the host port to the container port + volumes: + - : # Attach a volume to a specific location in the container + environment: + - = # Environment variables to be passed to the container +``` + +### **Explanation**: +- **`services:`**: The core section where you define different services (containers) that make up your application. +- **``**: Replace with the name of the service (e.g., `web`, `database`). Each service corresponds to a container. +- **`container_name`**: The name given to the container. +- **`image`**: The Docker image used to run the service (e.g., `python:3.9`, `nginx`). +- **`restart: always`**: Ensures the container will always restart if it stops, providing higher availability. +- **`ports`**: Maps ports from the host to the container, allowing the container to be accessed externally. The syntax is `:`. +- **`volumes`**: Links a Docker volume or host directory to a directory inside the container, enabling persistent data or sharing of resources. Example: `myvolume:/usr/src/app`. +- **`environment`**: Defines environment variables to be passed to the container at runtime. For example, setting an environment variable like `DB_HOST=localhost`. + +--- + +## **2. Defining Volumes** + +Docker Compose allows you to define persistent volumes that can be attached to containers. Here's how to define a volume: + +```yaml +volumes: + : # Define the volume here +``` + +### **Explanation**: +- **`volumes:`**: This section allows you to define named volumes that can be used in the services. +- **``**: Replace this with the name of the volume (e.g., `data-volume`, `db-volume`). The volume can be attached to different services to persist data beyond the container's lifecycle. + +--- + +## **3. Useful Docker Compose Commands** + +### **3.1 Start the Docker Compose Application** + +To bring up the application defined in the `docker-compose.yml` file, use: + +```bash +docker compose up +``` + +### **3.2 Run Docker Compose in Detached Mode (Background)** + +To run your Docker Compose services in the background (detached mode): + +```bash +docker compose up -d +``` + +### **3.3 Stop and Remove Docker Compose Services** + +To stop the services and remove the containers, networks, and volumes created by Docker Compose: + +```bash +docker compose down +``` + +### **Explanation of Commands**: +- **`docker compose up`**: Builds, (re)creates, and starts all services defined in the `docker-compose.yml` file. +- **`docker compose up -d`**: Runs the services in the background, keeping your terminal free while the containers continue running. +- **`docker compose down`**: Stops and removes all running services, containers, and networks created by Docker Compose. You can add the `-v` flag to remove volumes as well.