How to dockerize your Python Django application for AWS Fargate?
The biggest game-changer for Docker on AWS was the announcement of AWS Fargate. Operating Docker containers could not be easier. With AWS Fargate, you launch Docker containers in the cloud without any need to manage virtual machines. Django is a popular Python web framework that encourages rapid development and clean, pragmatic design.
The following post describes how you can dockerize your Python Django application and run it on AWS Fargate.
Building the Docker images
Two Docker images are needed:
- NGINX to serve static files and proxy to Django
- Python Django application
First, you will learn how to build NGINX image. The Dockerfile
makes use of multi-stage builds. You can use more than one FROM
statement in your Dockerfile, as shown in the following example.
- Static assets are generated in a Python stage
- Based on the official python image
- Using
pip3
to install the Python dependencies - Copying the app
- Generating the static assets with
python3 manage.py collectstatic
(output goes to theassets
folder)
- The static assets are copied (using
COPY --from=build
) into the NGINX stage which produces the final Docker image- Based on the official nginx image
- Copying the
static/
folder from the previous stage
Customization Most likely, your folder structure is different. Therefore, adapt the Copy Python files section in the following Dockerfile to your needs.
# Static Assets |
The NGINX configuration file forwards requests to the Python container if the path does not start with /static/
.
server { |
Next, you will learn how to create the Django image. The Dockerfile
is based on the official python image with the following additions:
- wait-for-it is installed to wait for the MySQL database container if you test locally.
- Python dependencies are installed with
pip3 install
. - A custom etrypoint is defined to run commands before the Django app starts (read on to learn more).
- gunicorn runs the app.
Customization Most likely, your folder structure is different. Therefore, adapt the Copy Python files section in the following Dockerfile to your needs.
FROM python:3.7.4 |
Customization The
-w
parameter of gunicorn defines the number of workers and should be in the range of2-4 x $(NUM_CORES)
.
The custom entrypoint is used to:
- Wait for the MySQL container if the
WAIT_FOR_IT
environment variable is set (used for testing locally only). - Run the database migrations before the Django app is started.
|
That’s it. You have everything you need to build both, the NGINX as well as the Django image. Next, you will learn how to test your containers and application locally.
Testing locally
Use Docker Compose to run your application locally. The following docker-compose.yml
file configures Docker Compose and starts three containers: NGINX, Django as well as a MySQL database.
version: '3' |
The following command starts the application:
docker-compose -f docker/docker-compose.yml up --build |
Magically, Docker Compose will spin up three containers: NGINX, Django, and MySQL. Point your browser to http://localhost:8080 to check that your web application is up and running. The log files of all containers will show up in your terminal, which simplifies debugging a lot.
After you have verified that your application is working correctly, cancel the running docker-compose
process by pressing CTRL + C
, and tear down the containers:
docker-compose -f docker/docker-compose.yml down |
Deploying on AWS
You are now ready to deploy your application on AWS.
(1) Build Docker images:
docker build -t python-django-nginx:latest \ |
(2) Create ECR repositories:
aws ecr create-repository --repository-name python-django-nginx \ |
(3) Login to Docker registry (ECR):
$(aws ecr get-login --no-include-email) |
(4) Tag Docker images:
docker tag python-django-nginx:latest \ |
(5) Push Docker images:
docker push \ |
There is only one step missing: you need to spin up the cloud infrastructure.
- Use our Free Templates for AWS CloudFormation.
- Use our cfn-modules.