
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”
Prerequisites:
- Docker installed on your machine (version 20.10 or later)
- Docker Compose installed on your machine (comes bundled with Docker)
- Basic knowledge of Docker and containerization. Refer to docker guides for more details on how to get started.
Step 1: Create the Project Structure
Create a new directory for your project and add the following subdirectories:
mkdir myapp
cd myapp
mkdir app redis nginx
Step 2: Create the Flask Application
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)
Step 3: Create the Redis Configuration
In the redis
directory, create a new file called redis.conf
with the following content:
port 6379
Step 4: Create the Nginx Configuration
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)
Step 5: Create the Dockerfiles
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)
Step 6: Create the Docker Compose File
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:
- app
Code language: JavaScript (javascript)
Step 7: Let’s Run the Multi-Container Docker Application!
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:
- If port 80 is occupied by another service running on your machine, change the Nginx port from “80” to something else(e.g., “8000,” etc.).
- If you are confused about whether to use “docker-compose ” instead of “docker-compose,” please refer to the forum discussion for more information.
Conclusion
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.
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
Leave a Reply