Using Django with Docker for Local Development

Dec. 19, 2024



Docker simplifies local development by creating isolated and reproducible environments for your projects. Using Docker with Django ensures that your application runs consistently across different systems, eliminates dependency conflicts, and makes it easier to collaborate with team members.

This guide explains how to set up Django with Docker for local development, covering the creation of Docker images, setting up services, and running your application.


Benefits of Using Docker for Django Development

  1. Consistency: Ensures your application runs the same across all environments.
  2. Isolation: Keeps dependencies and configurations separate from your local machine.
  3. Portability: Easy to share the development environment with others.
  4. Simplified Setup: Quickly spin up required services like databases using pre-configured containers.

Prerequisites

  • Install Docker: Download and install Docker Desktop from docker.com.
  • Basic knowledge of Docker concepts (containers, images, Dockerfile).

Step 1: Create a Django Project

If you already have a Django project, skip this step. Otherwise, create a new project:

bash
django-admin startproject myproject cd myproject

Step 2: Write a Dockerfile

A Dockerfile defines the environment for your Django application. Create a file named Dockerfile in your project root directory and add the following content:

dockerfile
# Use the official Python image as a base FROM python:3.10-slim # Set the working directory WORKDIR /app # Copy the requirements file COPY requirements.txt /app/ # Install dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy the Django project into the container COPY . /app/ # Expose the port the app runs on EXPOSE 8000 # Default command to run the application CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Step 3: Create requirements.txt

Generate the requirements.txt file if you haven’t already:

bash
pip freeze > requirements.txt

Step 4: Set Up docker-compose.yml

Docker Compose allows you to manage multi-container applications (e.g., Django + PostgreSQL). Create a docker-compose.yml file in your project root:

yaml
version: "3.9" services: web: build: context: . ports: - "8000:8000" volumes: - .:/app environment: - DEBUG=True depends_on: - db db: image: postgres:15 environment: POSTGRES_USER: myuser POSTGRES_PASSWORD: mypassword POSTGRES_DB: mydatabase volumes: - postgres_data:/var/lib/postgresql/data volumes: postgres_data:

Step 5: Update settings.py for Docker

Modify your settings.py to use the PostgreSQL database from the Docker setup. Replace the DATABASES section with:

python
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'myuser', 'PASSWORD': 'mypassword', 'HOST': 'db', 'PORT': 5432, } }

Step 6: Build and Run the Containers

  1. Build the Docker image:

    bash
    docker-compose build
  2. Start the containers:

    bash
    docker-compose up
  3. Access the Django application at http://localhost:8000.


Step 7: Run Database Migrations

To initialize your database, run migrations inside the running Docker container:

bash
docker-compose exec web python manage.py migrate

Step 8: Optional - Create a Superuser

Create an admin user for the Django admin panel:

bash
docker-compose exec web python manage.py createsuperuser

Step 9: Manage Static Files

During development, configure static files to work seamlessly. Update the settings.py file:

python
STATIC_URL = '/static/' STATIC_ROOT = '/app/staticfiles'

Run the collectstatic command:

bash
docker-compose exec web python manage.py collectstatic

Step 10: Debugging and Logs

To view logs from your containers, use:

bash
docker-compose logs -f

To stop the containers, run:

bash
docker-compose down

Folder Structure After Setup

Your project structure should look like this:

markdown
myproject/ ├── Dockerfile ├── docker-compose.yml ├── manage.py ├── myproject/ │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ ├── wsgi.py ├── requirements.txt

Advantages of Using Docker with Django

  1. Simplified Collaboration: Share the docker-compose.yml file, and your team can replicate the setup easily.
  2. Integrated Services: Add other services like Redis or Celery by updating docker-compose.yml.
  3. No Dependency Conflicts: Different projects can use different versions of Python or Django without interference.

Conclusion

Docker revolutionizes local development by providing an isolated and consistent environment for Django projects. By following this guide, you can set up a Docker-based Django environment that simplifies local development, testing, and deployment.