Day 18: Docker Project for DevOps Engineers (Part-2)
DevOps Learning

What is Docker Compose?
Docker Compose is a powerful tool for defining and managing multi-container applications. It simplifies the orchestration of your entire application stack, making it easy to manage services, networks, and volumes—all within a single, comprehensible YAML configuration file. With Docker Compose, you can define your application’s services, their relationships, and their configurations in a structured way.

Here are some key points about Docker Compose:
Multi-Container Applications: Docker Compose is specifically designed for multi-container applications. It allows you to define multiple services (containers) that work together as part of your application.
YAML Configuration: You express your application’s structure and settings in a YAML file (usually named
docker-compose.yml). This file defines services, networks, volumes, and other configurations.Streamlined Development and Deployment: Docker Compose streamlines development by allowing you to spin up your entire application stack with a single command. It’s equally useful for staging, testing, and production environments.
Lifecycle Management: Compose provides commands to start, stop, rebuild, and view the status of services. You can also stream log output and run one-off commands on specific services.
Writing a docker-compose.yml File
Let’s create a simple example using Docker Compose. We’ll build a basic Python web application that uses Flask and Redis. The application will have a hit counter stored in Redis. Follow these steps:
Step 1: Set Up Your Project
Create a directory for your project (let’s call it webApp):
mkdir webApp cd webAppCreate a file called
app.pyin your project directory and paste the following Python code:import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): count = get_hit_count() return f'Hello World! I have been seen {count} times.\n'Create a file called
requirements.txtin your project directory and add the following lines:flask redisCreate a
Dockerfilewith the following content:FROM python:3.10-alpine WORKDIR /code ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 RUN apk add --no-cache gcc musl-dev linux-headers COPY requirements.txt requirements.txt RUN pip install -r requirements.txt EXPOSE 5000 COPY . . CMD ["flask", "run", "--debug"]
Step 2: Create the docker-compose.yml File
Create a file named docker-compose.yml in your project directory and add the following content:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
In this example:
We define two services:
webandredis.The
webservice builds from the current directory (using theDockerfile).The
webservice maps port 5000 on the host to port 5000 in the container.The
redisservice uses the official Redis image from Docker Hub.
Step 3: Run Your Application
Execute the following command to start your application:
docker-compose up
Your Python web app will be accessible at http://localhost:5000.

You can also find that project at:
https://github.com/skay49/docker-compose.git
And there you have it! You’ve created a simple multi-container application using Docker Compose. Feel free to explore more complex scenarios and customize your docker-compose.yml file based on your specific needs. 😊
Thank you for reading😉.



