Docker Compose is a tool that allows you to easily create, manage, and scale Docker containers. It provides a simple way to define and run multi-container applications, making it ideal for building and deploying complex web applications.
In this tutorial, we will show you how to set up a Docker Compose environment with Django and FastAPI apps, and how to use Nginx as a reverse proxy to route traffic to the respective apps based on the URL path. This setup allows you to have different services running on the same server and serve them through a single domain.
Prerequisites:
- Docker installed on your machine
- Basic understanding of Docker Compose
- Basic knowledge of Django and FastAPI frameworks
- Familiarity with Nginx configuration
Step 1: Create Django and FastAPI apps
First, let’s create our Django and FastAPI applications. You can use the following commands to create a new Django project and a new FastAPI project:
For Django:
$ django-admin startproject my_django_app
For FastAPI:
$ pip install fastapi
$ pip install uvicorn
$ mkdir my_fastapi_app
$ cd my_fastapi_app
$ touch main.py
Step 2: Create Dockerfiles for Django and FastAPI
Next, we need to create Dockerfiles for our Django and FastAPI apps. These Dockerfiles will define the environment and dependencies for each app.
For Django, create a Dockerfile
with the following content:
FROM python:3.8
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
For FastAPI, create a Dockerfile
with the following content:
FROM python:3.8
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
Step 3: Create Docker Compose file
Now, let’s create a docker-compose.yml
file to define our services. We will have three services: Django, FastAPI, and Nginx.
version: '3'
services:
django:
build:
context: .
dockerfile: Django/Dockerfile
ports:
- '8000:8000'
fastapi:
build:
context: .
dockerfile: FastAPI/Dockerfile
ports:
- '8001:8001'
nginx:
image: nginx:latest
ports:
- '80:80'
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
Step 4: Create Nginx configuration file
We need to create an nginx.conf
file to configure Nginx as a reverse proxy that routes traffic to the Django and FastAPI apps based on the URL path.
http {
server {
listen 80;
location /django/ {
proxy_pass http://django:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /fastapi/ {
proxy_pass http://fastapi:8001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Step 5: Run the Docker Compose environment
Now that we have all our configurations in place, let’s run our Docker Compose environment:
$ docker-compose up --build
This command will build and start the Django, FastAPI, and Nginx services. You should be able to access the apps through the following URLs:
- Django app: http://localhost/django/
- FastAPI app: http://localhost/fastapi/
That’s it! You have now successfully set up a Docker Compose environment with Django and FastAPI apps, and Nginx as a reverse proxy to route traffic to the respective apps based on the URL path. This setup allows you to easily manage and scale your applications, making deployment and maintenance a breeze.