0
0
DockerHow-ToBeginner · 3 min read

How to Create a Dockerfile for Django: Simple Guide

To create a Dockerfile for Django, start with a Python base image, copy your project files, install dependencies from requirements.txt, and set the command to run the Django server. This setup packages your Django app into a container for easy deployment.
📐

Syntax

A basic Dockerfile for Django includes these parts:

  • FROM: Selects the base image, usually Python.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies your project files into the container.
  • RUN: Runs commands like installing dependencies.
  • EXPOSE: Opens the port Django will use.
  • CMD: Defines the command to start the Django server.
dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
💻

Example

This example Dockerfile builds a container for a Django app. It uses Python 3.11 slim image, installs dependencies, copies the app code, exposes port 8000, and runs the Django development server.

dockerfile
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Copy dependency file and install
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy project files
COPY . .

# Expose port 8000 for Django
EXPOSE 8000

# Run Django development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Output
Successfully built image Container runs Django server accessible on port 8000
⚠️

Common Pitfalls

Common mistakes when creating a Dockerfile for Django include:

  • Not copying requirements.txt before installing dependencies, causing cache misses.
  • Forgetting to expose the correct port (usually 8000).
  • Running the server on localhost (127.0.0.1) instead of 0.0.0.0, making it inaccessible outside the container.
  • Not setting a working directory, which can cause file path errors.
dockerfile
WRONG:
FROM python:3.11-slim
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]

RIGHT:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
📊

Quick Reference

Remember these tips when writing your Django Dockerfile:

  • Use a lightweight Python base image like python:3.11-slim.
  • Set WORKDIR to organize files inside the container.
  • Copy requirements.txt first to leverage Docker cache.
  • Expose port 8000 and run the server on 0.0.0.0.
  • Use pip install --no-cache-dir to keep image size small.

Key Takeaways

Start your Dockerfile with a Python base image and set a working directory.
Copy requirements.txt first and install dependencies to optimize build caching.
Expose port 8000 and run Django server on 0.0.0.0 to allow external access.
Use lightweight images and clean pip installs to keep your container small.
Avoid common mistakes like wrong server binding or missing working directory.