DEV Community

Anna lilith
Anna lilith

Posted on

Python Docker Containerization: From Dev to Production

Python Docker Containerization: From Dev to Production

Tags: python, docker, devops, deployment

Why Docker for Python?

Docker eliminates "works on my machine" problems. Package your Python app with all dependencies into a portable container.

Minimal Dockerfile

# Multi-stage build for smaller images
FROM python:3.12-slim as builder

WORKDIR /app
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt

FROM python:3.12-slim
WORKDIR /app

# Copy only what's needed
COPY --from=builder /root/.local /root/.local
COPY . .

# Set PATH for installed packages
ENV PATH=/root/.local/bin:$PATH
ENV PYTHONUNBUFFERED=1

EXPOSE 8000
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0"]
Enter fullscreen mode Exit fullscreen mode

Docker Compose for Multi-Service Apps

version: '3.8'
services:
  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/app
      - REDIS_URL=redis://redis:6379
    depends_on:
      - db
      - redis
    volumes:
      - ./data:/app/data

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: app
    volumes:
      - pgdata:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  pgdata:
Enter fullscreen mode Exit fullscreen mode

Best Practices

1. Use Multi-Stage Builds

# Build stage
FROM python:3.12 AS builder
RUN pip install --user -r requirements.txt

# Runtime stage (smaller, no build tools)
FROM python:3.12-slim
COPY --from=builder /root/.local /root/.local
Enter fullscreen mode Exit fullscreen mode

2. Minimize Image Size

# Good: slim base
FROM python:3.12-slim

# Bad: full image
FROM python:3.12
Enter fullscreen mode Exit fullscreen mode

3. Order Layers Correctly

# Copy requirements first (cached layer)
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy code last (changes often)
COPY . .
Enter fullscreen mode Exit fullscreen mode

4. Use .dockerignore

__pycache__
*.pyc
.git
.env
*.md
tests/
Enter fullscreen mode Exit fullscreen mode

Production Checklist

  • [ ] Multi-stage build
  • [ ] Non-root user
  • [ ] Health check endpoint
  • [ ] Proper logging (stdout)
  • [ ] Environment variables for config
  • [ ] Graceful shutdown handling

Related Products

Need pre-built Docker templates and deployment scripts? Check out our containerization packages.

Browse the collection →

Top comments (0)