← Back to Blog

Getting Started with Docker for Python Developers

January 15, 20252 min read
DockerPythonDevOps

Why Docker Changed How I Work

Before Docker, deploying a Django app meant wrestling with virtualenvs, system Python versions, and the classic "works on my machine" problem. Containers solved all of that by giving me a reproducible, versioned runtime from development to production.

Writing Your First Dockerfile

Here's a minimal, production-pattern Dockerfile for a Django app:

dockerfileFROM python:3.11-slim

WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]

This keeps the image small, avoids writing .pyc files, and uses Gunicorn for a production-friendly server.

Docker Compose for Django + Postgres

For local development, compose makes wiring services painless:

yamlversion: "3.9"
services:
  web:
    build: .
    ports:
      - "8000:8000"
    env_file:
      - .env
    depends_on:
      - db
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: app
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Now docker compose up spins everything up with a single command.

Production Tips

  • Use a multi-stage Dockerfile for slimmer images.
  • Pin Python and dependency versions.
  • Run as a non-root user.
  • Use environment variables for secrets.

Final Thoughts

Start small: Dockerize one service, then scale to multi-service stacks. Once you have repeatable containers, CI/CD and deployments become dramatically simpler.

Enjoyed this post? Let's connect.

Follow along for more Python, Django, and DevOps deep dives.