Simplifying Django Development with Docker and Docker Compose

Docker is an open-source platform that allows developers to automate and streamline the process of building, packaging, and deploying applications in self-contained environments called containers. One of the most popular web frameworks, Django, can greatly benefit from Dockerization. We will discuss the advantages of using Docker for Django development and walk through the process of setting up a Docker Environment for a Django project.

Advantages of Dockerizing Django

  1. Consistent environment: Docker ensures a consistent environment across different stages of the development lifecycle, eliminating the “it works on my machine” issue. Containers have all the dependencies required for an application, making it easy to share and run the application on any system with Docker installed.
  2. Simplified dependency management: Docker allows you to manage all your project dependencies in a single Dockerfile, reducing the complexity of managing different dependencies across multiple environments.
  3. Scalability: Docker’s lightweight containers make it easier to scale applications horizontally. With Docker Compose, you can define a multi-container application, allowing for seamless scaling of individual components.
  4. Faster onboarding: New team members can quickly set up their development environments by pulling the repository with Docker configuration, building the image and running the containers, saving time and effort.

Setting up a Dockerized Django project

Install Docker and Docker Compose

First, make sure you have Docker and Docker Compose installed on your system. You can download them from the official Docker website ( and follow the installation instructions for your specific operating system.

Create a Django project directory

Create a new directory for your Django project and navigate to it:

mkdir my_django_project
cd my_django_project

Create a Dockerfile

Create a Dockerfile in the project directory with the following content:

# Use the official Python image as the base image
FROM python:3.10

# Set the working directory

# Copy the requirements file into the container
COPY requirements.txt .

# Install the dependencies
RUN pip install -r requirements.txt

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on

# Start the application
CMD ["python", "", "runserver", ""]

Create a requirements.txt file

Create a requirements.txt file in the project directory with the following content:


Feel free to add any other dependencies your project may require.

Create a docker-compose.yml file

Create a docker-compose.yml file in the project directory with the following content:

version: '3.8'

    build: .
      - .:/app
      - "8000:8000"

This configuration defines a single service named ‘web’ that will build and run our Django application.

Initialize the Django project

Run the following command to create a new Django project inside the container:

docker-compose run web django-admin startproject mysite .

This command will create a new Django project named ‘mysite’ in your current directory.

Run the Django development server

Start the Django development server by running:

docker-compose up

Access the Django app by navigating to http://localhost:8000 in your web browser. You should see the default Django welcome page.

Make database migrations

If you need to set up a database for your Django project, you can easily configure it in the docker-compose.yml file. For instance, let’s add a PostgreSQL database:

version: '3.8'

    image: postgres:13.5-alpine
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydb
    build: .
    command: python runserver
      - .:/app
      - "8000:8000"
      - db

Update your requirements.txt file to include the PostgreSQL adapter for Django:


Then, modify the DATABASES setting in your mysite/ file to use the PostgreSQL database:

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'db',
        'PORT': 5432,

Run the following commands to apply the database migrations:

docker-compose down
docker-compose up --build -d
docker-compose run web python makemigrations
docker-compose run web python migrate

Now, your Django project is connected to a PostgreSQL database, and you can start developing your application.