In this tutorial, we’ll walk through the process of containerizing a multi-container Docker application using “Docker Compose.” We’ll create a simple web application consisting of a Python Flask backend, a Redis database, and an Nginx web server.
Note: This post has been updated to reflect the usage change from “fig” , which is the predecessor to “docker-compose”
Create a new directory for your project and add the following subdirectories:
mkdir myapp
cd myapp
mkdir app redis nginx In the app directory, create a new file called app.py with the following content:
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)Code language: JavaScript (javascript) In the redis directory, create a new file called redis.conf with the following content:
port 6379 In the nginx directory, create a new file called nginx.conf with the following content:
http {
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://app:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}Code language: PHP (php) Create a new file called Dockerfile in each of the app, redis, and nginx directories:
Location: app/Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]Code language: JavaScript (javascript) Location: redis/Dockerfile
FROM redis:alpine
COPY redis.conf /etc/redis/redis.conf
CMD ["redis-server", "/etc/redis/redis.conf"]Code language: JavaScript (javascript) Location: nginx/Dockerfile
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]Code language: JavaScript (javascript) In the main application root directory, create a new file called docker-compose.yml (or just compose.yml if you are using the new “docker compose” command) with the following content:
services:
app:
build: ./app
ports:
- "5000:5000"
depends_on:
- redis
environment:
- REDIS_HOST=redis
redis:
build: ./redis
ports:
- "6379:6379"
nginx:
build: ./nginx
ports:
- "80:80"
depends_on:
- appCode language: JavaScript (javascript) Run the following command to build and start the containers:
docker-compose up
#or
docker compose up
Code language: CSS (css) Open your web browser and navigate to http://localhost to see the application in action.
Note:
In this tutorial, we’ve successfully orchestrated a multi-container application using Docker and Docker Compose. We’ve created a Flask application, a Redis database, and an Nginx web server, and demonstrated how to manage and orchestrate them using Docker Compose. Also, as you continue developing your application, don’t forget to auto-reload your docker environment. If there are any follow-up questions/comments, please comment below.
Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…
Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…
You've conquered the service worker lifecycle, mastered caching strategies, and explored advanced features. Now it's time to lock down your implementation with battle-tested service worker…
This website uses cookies.
View Comments