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 (https://www.docker.com/) 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
WORKDIR /app

# 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
EXPOSE 8000

# Start the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Create a requirements.txt file

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

Django

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'

services:
  web:
    build: .
    volumes:
      - .:/app
    ports:
      - "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'

services:
  db:
    image: postgres:13.5-alpine
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydb
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db

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

Django
psycopg2

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

DATABASES = {
    '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 manage.py makemigrations
docker-compose run web python manage.py migrate

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