☑️Day 28: Exploring Docker Compose

☑️Day 28: Exploring Docker Compose

🔹Table of Contents :

  1. Introduction to Docker Compose

  2. Key Concepts in Docker Compose

  3. Basic Docker Compose Commands

    • docker-compose up

    • docker-compose down

    • docker-compose build

    • docker-compose ps

    • docker-compose logs

  4. Understanding YAML for Docker Compose

  5. Real-Time Scenario: Multi-Service Web Application

  6. Advanced Docker Compose Features

  7. Real-World Use Case: DevOps Testing Environment

  8. CI/CD Pipeline with Docker Compose

  9. Scaling Applications with Docker Compose

  10. Key Concepts

Introduction: Docker Compose is a tool for defining and running multi-container Docker applications. It helps orchestrate multiple services that depend on each other, making it perfect for setting up complex environments.


✅1. What is Docker Compose?

  • Docker Compose is a tool that defines and runs multi-container Docker applications.

  • With a simple YAML file, you can specify all services, networks, and volumes required.


✅2. Key Concepts in Docker Compose:

  • Services: Define different containers (like web, database).

  • Networks: Manage how containers communicate.

  • Volumes: Persistent storage for containers.


✅3. Basic Docker Compose Commands:

  • docker-compose up: Builds, (re)creates, starts, and attaches to containers.

      bashCopy codedocker-compose up
    
  • docker-compose down: Stops containers and removes containers, networks, volumes, and images created by up.

      bashCopy codedocker-compose down
    
  • docker-compose build: Builds or rebuilds services defined in the Docker Compose file.

      bashCopy codedocker-compose build
    
  • docker-compose ps: Lists the containers defined in the YAML.

      bashCopy codedocker-compose ps
    
  • docker-compose logs: Displays the logs of running containers.

      bashCopy codedocker-compose logs
    

✅4. YAML Basics for Docker Compose:

A docker-compose.yml file defines services, networks, and volumes.

Example: Basic YAML structure

yamlCopy codeversion: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example
  • version: Version of Docker Compose.

  • services: Define containers (web, db).

  • ports: Map host ports to container ports.

  • environment: Set environment variables for containers.


✅5. Real-Time Scenario: Multi-Service Web App

Imagine a scenario where a team is developing a web application with the following components:

  • A Node.js backend.

  • A React frontend.

  • A MySQL database.

Instead of running each service individually, Docker Compose simplifies this by defining all components in one file.

docker-compose.yml Example:

yamlCopy codeversion: '3.8'
services:
  frontend:
    image: node:14
    working_dir: /app
    volumes:
      - .:/app
    command: npm start
    ports:
      - "3000:3000"
  backend:
    image: node:14
    working_dir: /app
    volumes:
      - ./backend:/app
    command: npm run dev
    ports:
      - "5000:5000"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"
volumes:
  db_data:
``  
- **frontend & backend:** Two services for the app’s user interface and business logic.
- **db:** MySQL database with persistent storage using volumes.
- **volumes:** Persistent storage so that data is not lost if the database container is removed.

---

### **6. Real-World Use Case: DevOps Testing Environment**

#### **Scenario:**
- A company needs to set up an isolated testing environment for multiple applications (web app, database, cache).
- The QA team wants to easily bring up/down the whole environment.

Using Docker Compose, they define the web app, database, and Redis cache in one `docker-compose.yml`. This allows the team to:
1. Spin up the entire environment with one command: `docker-compose up`.
2. Simulate production environments for testing with version-specific dependencies.
3. Scale services easily for testing under different loads.

#### **Advanced Concepts Used:**
- **Multi-Stage Build:** For optimizing image size by separating the build process.
- **Environment-Specific Files:** Separate `docker-compose` files for development, testing, and production environments using overrides (e.g., `docker-compose.override.yml`).

---

### **7. Mid-Level DevOps Use Case: CI/CD Pipeline with Docker Compose**

#### **Scenario:**
- You're deploying a microservices architecture, and each service is containerized.
- Docker Compose is used in the CI/CD pipeline to test the integration of different services.

1. Define the service dependencies (API, web, database) in the `docker-compose.yml` file.
2. Use Compose in Jenkins or GitLab CI/CD pipelines to spin up all containers and run integration tests.
3. Stop and clean up all services with `docker-compose down` after the pipeline is done.

---

### **8. Scaling with Docker Compose:**
In real-world projects, Docker Compose can scale services, enabling you to add more instances of a service easily.

- **docker-compose scale:**
  ```bash
  docker-compose up --scale web=3

This creates 3 instances of the web service.


✅Key Commands:

  • docker-compose up: Start the multi-container app.

  • docker-compose down: Stop and clean up the app.

  • docker-compose ps: List running containers.

  • docker-compose logs: View container logs.

  • docker-compose build: Build images.

  • Advanced: docker-compose.override.yml for environment-specific configurations.


🚀Thanks for joining me on Day 28! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

#DevOps #Docker #Dockerfile #Docker-compose #Containerization #CloudComputing #TechLearning #ShubhamLonde #Day28

💡
Follow for more updates on LinkedIn , Github and Twitter(X)