Day 04: Docker Compose: Managing multi-container applications

As your projects grow in complexity, managing multiple containers manually can become a challenge. This is where Docker Compose comes into play. In this article, we’ll explore what Docker Compose is, its benefits, and how to use it to simplify the orchestration of multi-container applications. What is Docker Compose? Docker Compose is a tool that allows you to define and manage multi-container Docker applications using a simple YAML configuration file. Instead of running multiple docker run commands, you can use Docker Compose to start, stop, and manage all the containers in your application with a single command. Key Benefits of Docker Compose I. Simplified Configuration: Define all containers, networks, and volumes in a single docker-compose.yml file. II. Dependency Management: Docker Compose ensures containers start in the correct order, respecting dependencies. III. Ease of Use: Manage the lifecycle of multi-container applications with commands like docker-compose up and docker-compose down. VI. Consistency Across Environments: Use the same configuration file for development, testing, and production. Getting Started with Docker Compose Prerequisites Docker Compose comes pre-installed with Docker Desktop on Windows and macOS. Linux users can install it separately by following the Docker Compose installation guide. Verify the installation by running: docker-compose --version Example: A Multi-Container Application Let’s create a simple application with the following components: A Python Flask web application. A Redis database. 1. Define the docker-compose.yml File Create a file named docker-compose.yml in your project directory: version: '3.9' services: web: build: . working_dir: /app volumes: - .:/app ports: - "5000:5000" depends_on: - redis redis: image: redis:alpine 2. Add the Flask Application Code Create a file named app.py in the same directory: from flask import Flask import redis app = Flask(__name__) client = redis.Redis(host="redis", port=6379) @app.route("/") def hello(): client.incr("hits") return f"Hello, Docker Compose! This page has been viewed {client.get('hits').decode()} times." if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) 3. Write a Dockerfile Here’s a simple Dockerfile to containerize our Python application: FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy requirements.txt into the container COPY requirements.txt . # Install dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy application code into the container COPY . . # Set the default command to run the application CMD ["python", "app.py"] 4. Adding requirements.txt Ensure your dependencies are listed in a requirements.txt file. For instance: flask redis 5. Start the Application Run the following command in the terminal: docker-compose up Docker Compose will: Pull the required images. Create and start the containers. Display logs for all containers. 5. Access the Application Open your browser and navigate to http://localhost:5000. You’ll see the Flask app running, with Redis tracking the number of visits. Managing the Application with Docker Compose Stop the Application Use the following command to stop and remove containers, networks, and volumes: docker-compose down Running in Detached Mode To run containers in the background, add the -d flag: docker-compose up -d Viewing Logs View logs for all containers with: docker-compose logs Or for a specific service: docker-compose logs web Conclusion Docker Compose is a powerful tool for managing multi-container applications, simplifying deployment and orchestration. With just a single YAML file, you can define, manage, and scale your services effortlessly. In the next article, we’ll dive into Docker Use Cases, exploring real-world scenarios and best practices for utilizing Docker effectively. Stay tuned!

Jan 15, 2025 - 19:47
Day 04: Docker Compose: Managing multi-container applications

As your projects grow in complexity, managing multiple containers manually can become a challenge. This is where Docker Compose comes into play.

In this article, we’ll explore what Docker Compose is, its benefits, and how to use it to simplify the orchestration of multi-container applications.

What is Docker Compose?

Docker Compose is a tool that allows you to define and manage multi-container Docker applications using a simple YAML configuration file. Instead of running multiple docker run commands, you can use Docker Compose to start, stop, and manage all the containers in your application with a single command.

Key Benefits of Docker Compose

I. Simplified Configuration:

  • Define all containers, networks, and volumes in a single docker-compose.yml file.

II. Dependency Management:

  • Docker Compose ensures containers start in the correct order, respecting dependencies.

III. Ease of Use:

  • Manage the lifecycle of multi-container applications with commands like docker-compose up and docker-compose down.

VI. Consistency Across Environments:

  • Use the same configuration file for development, testing, and production.

Getting Started with Docker Compose

Prerequisites

  • Docker Compose comes pre-installed with Docker Desktop on Windows and macOS.
  • Linux users can install it separately by following the Docker Compose installation guide.

Verify the installation by running:

docker-compose --version

Checking Docker-Compose Version

Example: A Multi-Container Application

Let’s create a simple application with the following components:

  • A Python Flask web application.
  • A Redis database.

1. Define the docker-compose.yml File

Create a file named docker-compose.yml in your project directory:

version: '3.9'
services:
  web:
    build: .
    working_dir: /app
    volumes:
      - .:/app
    ports:
      - "5000:5000"
    depends_on:
      - redis

  redis:
    image: redis:alpine

2. Add the Flask Application Code

Create a file named app.py in the same directory:

from flask import Flask
import redis

app = Flask(__name__)
client = redis.Redis(host="redis", port=6379)


@app.route("/")
def hello():
    client.incr("hits")
    return f"Hello, Docker Compose! This page has been viewed {client.get('hits').decode()} times."


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

3. Write a Dockerfile

Here’s a simple Dockerfile to containerize our Python application:

FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy requirements.txt into the container
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code into the container
COPY . .

# Set the default command to run the application
CMD ["python", "app.py"]

4. Adding requirements.txt

Ensure your dependencies are listed in a requirements.txt file. For instance:

flask
redis

5. Start the Application

Run the following command in the terminal:

Starting the Application

docker-compose up

Docker-Compose Up

Docker Compose will:

  • Pull the required images.
  • Create and start the containers.
  • Display logs for all containers.

Stopping Using Ctrl+C

5. Access the Application

Open your browser and navigate to http://localhost:5000. You’ll see the Flask app running, with Redis tracking the number of visits.

Web-Page

Managing the Application with Docker Compose

Stop the Application

Use the following command to stop and remove containers, networks, and volumes:

docker-compose down

In Process of Stopping

Stopped

Running in Detached Mode

To run containers in the background, add the -d flag:

docker-compose up -d

Viewing Logs

View logs for all containers with:

docker-compose logs

Docker-Compose Logs

Or for a specific service:

docker-compose logs web

Docker-Compose logs web

Conclusion

Docker Compose is a powerful tool for managing multi-container applications, simplifying deployment and orchestration. With just a single YAML file, you can define, manage, and scale your services effortlessly.

In the next article, we’ll dive into Docker Use Cases, exploring real-world scenarios and best practices for utilizing Docker effectively. Stay tuned!