Skip to main content
This documentation provides a step-by-step guide to setting up a Python Django server and creating a Dockerfile to deploy it via LocalOps.

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-example

Create 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.
In the tutorial example we will be connecting to Postgres DB. Update your project settings.py file to refer the database details from environment variables.
django_todo/settings.py
Make sure you have already created the provided database with username and password.

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.
In this tutorial we will be adding REST CRUD (Create, Read, Update and Delete) endpoints for 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,
Run Django server,
Visit 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
Read more about .dockerignore here
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 deployment

Run DB migration inside docker image

In this tutorial, to keep DB migration step simple, we will create a entrypoint.sh script to run DB migrations each time the docker image is run.
entrypoint.sh
Optionally you can have a separate image to run your DB migrations. In production setup, it is advisable to have separate image to run DB migrations.

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 the source 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:
This command builds the 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.
This command requires PostgresDB to be running on localhost:54321.

Docker Compose

If you want to run PostgresDB alongside our Django image, then we can create a docker-compose.yml file with below content,
docker-compose.yml

Docker compose up

After running the command, Visit http://localhost:8000/api/v1/tasks/ to list the tasks. Hurray 🎉. Now we have created and Dockerized a Python Django app.

Deploy

You can now commit and push the Dockerfile to your git repo. Create a service now to point at the git repository and branch name to deploy this image.