Docker Compose Demystified: A Practical Guide to Multi-Container Application Management
Discover how Docker Compose simplifies the deployment of multi-container applications in this guide with an example
Docker has fundamentally transformed the landscape of application packaging and deployment, offering developers a streamlined approach to managing dependencies and ensuring uniform functionality across diverse environments. While Docker excels in simplifying the deployment of single-container applications, the complexity escalates when orchestrating multiple containers that must collaborate seamlessly. This is precisely where Docker Compose shines. In this blog post, we delve into the intricacies of Docker Compose, unveiling its pivotal role in orchestrating multi-container applications. We'll explore its capabilities in depth and furnish you with practical examples to kickstart your journey into the world of container orchestration.
Here are some of the cases where you might need to develop multi-container apps using Docker Compose:
- Consider creating a web application that requires a backend API server (made with Flask, Django, Node.js, etc.) and a database (such as MySQL, PostgreSQL, or MongoDB). Docker Compose enables you to create distinct containers for your backend service and database, ensuring that they interact easily while staying independent.
- CI/CD Pipelines: Docker Compose may be used to describe the application environment for each pipeline step (e.g., build, test, and deploy). This guarantees that the same environment used for development and testing is also utilized for deployment, reducing the likelihood of difficulties developing as a result of environment changes.
In this example, we will use Docker Compose to build a Flask application that communicates with a Redis database. Docker Compose enables us to design and execute several container apps from a single configuration file.
Try Kodaschool for free
Click below to sign up and get access to free web, android and iOs challenges.
Prerequisites:
- Both Docker and Docker Compose is installed on your system.
- Basic grasp of Python and Flask.
Create a new directory for the project and navigate into it.
mkdir flask-redis-app
cd flask-redis-app
Create a new Python file called app.py
and add the following code:
from flask import Flask
import redis
app = Flask(__name__)
redis_client = redis.Redis(host='redis', port=6379)
@app.route('/')
def index():
count = redis_client.incr('hits')
return f'This page has been viewed {count} times.'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
This Flask application connects to a Redis instance and increments a counter every time the root URL (/
) is accessed.
Next, Create a new file called requirements.txt
and add the following dependencies:
$ cat requirements.txtflask
redis
Next, Create a new file called Dockerfile
and add the following content:
$ cat Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
This Dockerfile sets up a Python environment, installs the required dependencies, copies the application code, and starts the Flask app.
Next step: Create a new file called docker-compose.yml
and add the following content:

This docker-compose.yml
file defines two services: web
(our Flask app) and redis
(the Redis database). The web
service builds the Flask app from the Dockerfile
, maps port 5000 on the host to port 5000 in the container, and depends on the redis
service. The redis
service uses the official Redis Docker image.
Next step: Build and run the multi-container application using Docker Compose, in the same location where docker-compose.yml file is
run this command docker-compose up --build
This command will build the Flask app image, pull the Redis image, and start both containers.

This command initiates the building process for the web service and pulls the redis:alpine image, starting both containers simultaneously. You'll observe the output of both services within your terminal. Next, open a web browser and visit http://localhost:5000. Here, you'll encounter a message reflecting the number of times the page has been visited, with the count increasing each time you refresh the page.
If everything runs correctly you will have this page,

It is a wrap!