Prerequisites
To follow this tutorial, you will need the following:- Python - v3.12.4 at the time of writing this doc.
- pip as a package manager for installing and maintaining dependencies. This usually comes bundled with Python > v3.4.
- Django - v5.0.7 at the time of writing this doc.
- [Docker] to build standalone containers to serve your Python app.
This guide assumes that you have basic knowledge of the above-mentioned technologies and tools. If you are not
familiar with any of them, it is recommended to review their official documentation and tutorials to get up to speed
before proceeding with this guide.
Example Python Django App
In this tutorial we can go over a simple to-do app that was created using Python Django. Source Code: https://github.com/localopsco/django-todo-exampleCreate Python Django app
You can also follow Django documentation to create a simple Django app.
Use python venv (Virtual Environment)
Install Django
using pip
Start a Django project
Start a Django app in the project
Connect to your DB
Please refer this Django article to connect to your
Database.
settings.py
file to refer the database details from environment variables.
django_todo/settings.py
Create DB models
You can refer this Django model article to know more on
creating DB models.
- Update the
todo/models.py
file with below content,
todo/models.py
Apply migrations
Add API endpoints
You can use Django REST Framework package to quickly bootstrap REST
endpoints.
Task
model using
Django REST Framework.
Copy paste the below contents to your respective files,
todo/views.py
todo/urls.py
django_todo/urls.py
Run server
Export the required environment variables,http://localhost:8000/api/v1/tasks/
to see the list of tasks served from our Django app.
Voila! You have successfully created and run Django Python server with REST CRUD endpoints for Task model. 🎉
Dockerize
Dockerizing makes the app run anywhere, agnostic of the platform. As long as Docker is installed, whether it’s Windows, Mac, or Linux, it can run with the same behavior.Create .dockerignore
Before creating the dockerfile, let’s create a.dockerignore
file and add that contents that should not be copied over
to the docker file system.
.dockerignore
Creating a
.dockerignore
file and adding folders like node_modules
is must, since dependencies will be installed
while building the image based on the platform preferences used. Copying those from file system will overwrite the
installed dependencies and might fail during deploymentRun DB migration inside docker image
In this tutorial, to keep DB migration step simple, we will create aentrypoint.sh
script to run DB migrations each
time the docker image is run.
entrypoint.sh
Create Dockerfile
Now, Create a Dockerfile. The Dockerfile is a text file that contains the instructions for Docker to build the image. The Dockerfile is posted for reference with steps to create the production image. Though docker supports Multi-Stage Builds we won’t be using that here since Python needs all thesource code
and dependencies to be present while running the application. You can opt for multi stage build,
if you have any build tool.
Dockerfile
Build docker image
Now you can build and run the Docker image. To build the Docker image:django-todo
image for platform linux/amd64
.
If you are locally testing your application, you can skip the platform
key to build the images
Run the docker image
Let’s run the Docker container using the image created of the react application with the command below.localhost:54321
.
Docker Compose
If you want to run PostgresDB alongside our Django image, then we can create adocker-compose.yml
file with below
content,
docker-compose.yml
Docker compose up
http://localhost:8000/api/v1/tasks/
to list the tasks.
Hurray 🎉. Now we have created and Dockerized a Python Django app.