Setting Up A Django App in Docker-Compose

Setting Up A Django App in Docker-Compose
Photo by Christopher Gower / Unsplash

One of the most popular web frameworks in 2026 (still) for full-stack web development is Django owing likely due to its flexibility with multiple frontend platforms, well constructed ORM and wide array of packages. Still, many developers don't know how to set up a Django application, or Docker. This is by no means a comprehensive guide (if you want that then go RTM), but rather a place to get you started. We'll start with Docker and then go into some Django specific settings.

Setting Up Docker

Docker is fairly easy to use but there can be challenges running other people's Docker configurations. Also, at least the last time I tried, running the same docker configurations between windows and UNIX based systems can give weird errors. This guide assumes you have docker already installed on your machine and that you are using a UNIX based system. I'm using a Macbook pro M1 and these configurations work on both the Mac and Ubuntu boxes.

First is the docker compose file. We'll go with a simple set up which is one container for the database and one container for the web application.

version: '3.7'
services:
  web:
    container_name: web
    restart: unless-stopped
    env_file: backend/core/settings/.env
    volumes:
      - ./backend:/app
    build:
      context: .
      dockerfile: Dockerfile
    command: bash entrypoint.sh
    depends_on:
      - db
    links:
      - db:db
  db:
    container_name: interface_db
    restart: unless-stopped
    image: postgres:16.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - backend/core/settings/.env

volumes:
  postgres_data:

Normally I will have two compose files, one local and one for production. Any number of compose files can be generated as needed. Alternately you can pass in environment variables via an environment file. If you want to pass in environment variables to the compose processor you will need to use a .env file in the SAME directory as the compose file. Let's walk through the steps here. Docker will build the images, and then you can run the containers. The db service is using a prebuilt postgres image so there are no custom configurations. For the web service, I am specifying that I want the builder to use the current directory as the context and that I have a specific Dockerfile for building the image. If desired the Dockerfile can be stored in a separate location to clean up the directory. This would turn the line "dockerfile: Dockerfile" line into "dockerfile: path/to/dockerfile/Dockerfile"

💡
To pass in environment variables to the compose processor you will need to use a .env file in the SAME directory as the compose file

The Dockerfile is where you will list the steps to build the docker image. This tells the builder what image to build and in what order the steps go.

FROM python:3.10.9-slim as compile-image

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
COPY install_packages.sh .
RUN bash install_packages.sh

RUN pip3 install --upgrade pip

# Install git for the one package that uses it..
RUN apt-get update
RUN apt-get -y install git

# Install Python Deps
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

FROM python:3.10.9-slim as build-image

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app

# Old Python Version Packages
COPY --from=compile-image /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --from=compile-image /usr/local/bin /usr/local/bin

RUN apt-get clean
RUN apt-get update
# Some of these are installed to get Postgres working with M1 Mac
RUN apt-get install -y libpq-dev libpango-1.0-0 libpangoft2-1.0-0
# Clean up extraneous stuff
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

COPY backend/local-entrypoint.sh /app/
COPY backend/entrypoint.sh /app/

Here we're using a multistage build to reduce the final image size. Django image sizes can be quite large if you use a single stage build. The first image is called compile-image. In this image, we are going to install some packages for the debian based slim python image. If you want to find out more about different python images available within docker, you can do so here. In the compile image I have grouped my shell install commands into a single install file. This is used to clean up the Dockerfile and make it more legible. Then we'll go through and install the required python packages. With our selection of database there are some additional packages to install. All of them are found in the requirements.txt file. There are a number of third-party packages available for Django that have been created over the years many of which are available here.

There's some copying packages over from the compile-image to the build-image, all of which is necessary, but the last piece is copying over the entrypoint scripts to the image to be able to run them on container creation. I lazily copy both the local and production entry points with the same Dockerfile but you could easily make two Dockerfiles, a production and local version and specify the one to use in the compose config.

A number of additional key features of the compose file are the links, and the environment file.

links:
  - db:db

This line in the web container configuration allows the web container to connect to the db container through the host "db." If you wanted to use a more secure or creative link name, that is up to you.

env_file: backend/core/settings/.env

Here I'm specifying an environment file to pass into the context of the docker service. Keen eyes will notice I passed the same one into the database service which is not really preferred, to have multiple environment variables floating around in services where they aren't needed. For the purpose of this exercise it is okay but I would not recommend this in production.

There is the volumes declaration which allows the database to persist after the compose services are stopped. You can bind mount standard directories of database containers to named volumes which is helpful if you have multiple volumes you want to be able to keep organized

Finally, this configuration file assumes that the Django project exists in the directory aptly titled "backend" which would then house your creative Django application.

Conclusion

To run this, a simple docker-compose up will initialize the services, build the image if it is the first time running, and start the services as requested. I highly recommend using some sort of containerization when developing as it will standardize your environment. This comes in handy if you work on multiple projects.

Cheers