Dockerfile

How to build a Dockerfile for a Python application.

A `Dockerfile` is a text file with instructions on how to build a Docker image.

Example Django Dockerfile

FROM python:3.13-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    default-libmysqlclient-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Set work directory
WORKDIR /app

# Install Python dependencies
# Using uv for speed
RUN pip install uv
COPY requirements.txt .
RUN uv pip install --no-cache-dir -r requirements.txt

# Copy project
COPY . .

# Expose port
EXPOSE 8000

# Run the application
CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "your_project.asgi:application"]
            
Key Instructions

`FROM`: Base image. `WORKDIR`: Sets the working directory. `COPY`: Copies files from host to image. `RUN`: Executes a command. `EXPOSE`: Informs Docker that the container listens on a specific port. `CMD`: Default command to run when the container starts.