Skip to main content
Frameworks

Flask Dockerfile

Dockerfile for frameworks projects

View on GitHub

Dockerfile Content

# ============================================================================
# Created by https://Dockerfile.io/
# COMPREHENSIVE FRAMEWORK TEMPLATE for Flask
# Website: https://flask.palletsprojects.com/
# Repository: https://github.com/pallets/flask
# ============================================================================

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# TEMPLATE OVERVIEW & USAGE NOTES
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# • TEMPLATE TYPE: COMPREHENSIVE FRAMEWORK TEMPLATE
# • PURPOSE: Production-ready Flask application with security hardening
# • DESIGN PHILOSOPHY: Self-contained with multi-stage builds and security
# • COMBINATION GUIDANCE: Use standalone for complete Flask applications
# • SECURITY CONSIDERATIONS: Non-root user, Alpine base, health monitoring
# • BEST PRACTICES: Layer caching, dependency optimization, production defaults
# • OFFICIAL SOURCES: Flask documentation and Docker security guidelines

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# BUILDER STAGE - Application compilation
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
FROM python:3.11-alpine AS builder

# Build arguments for environment configuration
ARG PYTHON_ENV=production
ARG BUILD_ID=unknown
ARG COMMIT_SHA=unknown

# Environment variables for build process
ENV PYTHON_ENV=${PYTHON_ENV} \
  BUILD_ID=${BUILD_ID} \
  COMMIT_SHA=${COMMIT_SHA} \
  PYTHONUNBUFFERED=1 \
  PYTHONDONTWRITEBYTECODE=1 \
  PIP_NO_CACHE_DIR=1 \
  PIP_DISABLE_PIP_VERSION_CHECK=1

# Set working directory
WORKDIR /app

# Install system dependencies
RUN apk add --no-cache \
  build-base \
  ca-certificates \
  postgresql-dev \
  zlib-dev \
  jpeg-dev \
  libffi-dev

# Copy dependency files first for optimal layer caching
COPY requirements*.txt ./

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
  pip install --no-cache-dir -r requirements.txt

# Copy application source code
COPY . .

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# RUNTIME STAGE - Production deployment
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
FROM python:3.11-alpine AS runtime

# Security: Create non-root user
RUN addgroup -g 1001 -S appgroup && \
  adduser -S -u 1001 -G appgroup appuser

# Production environment configuration
ENV PYTHON_ENV=production \
  PYTHONUNBUFFERED=1 \
  PYTHONDONTWRITEBYTECODE=1 \
  UMASK=0027 \
  FLASK_ENV=production \
  FLASK_APP=app.py

# Set working directory
WORKDIR /app

# Install runtime dependencies only
RUN apk add --no-cache \
  libpq \
  zlib \
  jpeg \
  libffi

# Copy installed dependencies from builder stage
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy application code with proper ownership
COPY --from=builder --chown=appuser:appgroup /app /app

# Create directories with secure permissions
RUN mkdir -p /app/logs /app/static /app/uploads && \
  chown -R appuser:appgroup /app/logs /app/static /app/uploads && \
  chmod 750 /app/logs /app/uploads

# Switch to non-root user
USER appuser

# Expose application port
EXPOSE 5000

# Health check for container orchestration
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD python -c "import urllib.request; \
  urllib.request.urlopen('http://localhost:5000/health')" || exit 1

# Default command with Gunicorn
ENTRYPOINT ["gunicorn"]
CMD ["--bind", "0.0.0.0:5000", "--workers", "3", "--worker-class", "sync", "app:app"]

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# DEVELOPMENT STAGE - Hot reload environment
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
FROM python:3.11-alpine AS development

WORKDIR /app

# Install system dependencies including dev tools
RUN apk add --no-cache \
  build-base \
  ca-certificates \
  postgresql-dev \
  zlib-dev \
  jpeg-dev \
  libffi-dev

# Copy dependency files
COPY requirements*.txt ./

# Install all dependencies (including dev dependencies)
RUN pip install --no-cache-dir --upgrade pip && \
  pip install --no-cache-dir -r requirements-dev.txt

# Copy source code
COPY . .

# Development environment variables
ENV FLASK_ENV=development \
  FLASK_APP=app.py \
  FLASK_DEBUG=1

# Development command with Flask runserver
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# ALTERNATIVE CONFIGURATIONS
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

# Option 1: Waitress server (Windows compatible)
# RUN pip install waitress
# CMD ["waitress-serve", "--port=5000", "app:app"]

# Option 2: Uvicorn with ASGI (for async Flask)
# RUN pip install uvicorn[standard]
# CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "5000"]

# Option 3: Gevent workers for async I/O
# CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "--worker-class", "gevent", "app:app"]

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# USAGE EXAMPLES & BEST PRACTICES
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

# Example 1: Build production image
# docker build --target runtime -t my-flask-app:prod .

# Example 2: Build development image
# docker build --target development -t my-flask-app:dev .

# Example 3: Build with custom arguments
# docker build \
#   --build-arg PYTHON_ENV=production \
#   --build-arg BUILD_ID=$(git rev-parse --short HEAD) \
#   -t my-flask-app:$(git rev-parse --short HEAD) .

# Example 4: Run production container
# docker run -d -p 5000:5000 --name flask-app my-flask-app:prod

# Example 5: Run development container with volume mount
# docker run -d -p 5000:5000 -v $(pwd):/app --name flask-dev my-flask-app:dev

# Best Practices:
# 1. Always use .dockerignore to exclude __pycache__, .git, etc.
# 2. Use specific Python versions (not 'latest')
# 3. Run health checks in production
# 4. Use multi-stage builds for smaller images
# 5. Set resource limits in docker-compose or Kubernetes

# Customization Notes:
# 1. Adjust Gunicorn workers based on available CPU cores
# 2. Modify exposed ports based on your application
# 3. Customize health check endpoint as needed
# 4. Add environment variables for database connections
# 5. Configure static file serving appropriately

Note: This file is fetched from GitHub and cached for 7 days.