11# syntax=docker/dockerfile:1.7
2- # Modern Dockerfile for python-project-template
3- # Features: multi -stage build, distroless prod, security scanning, BuildKit caching
2+ # Simplified Dockerfile for python-project-template
3+ # Single -stage development-focused build
44
55ARG PYTHON_VERSION=3.13.1
6- ARG BUILDPLATFORM=linux/amd64
76
8- # =============================================================================
9- # Base stage: Python + uv package manager
10- # =============================================================================
11- FROM --platform=$BUILDPLATFORM python:${PYTHON_VERSION}-alpine AS base
7+ FROM python:${PYTHON_VERSION}-slim AS base
128
13- # Install uv for ultra-fast Python package management
14- RUN --mount=type=cache,target=/root/.cache/pip \
15- pip install --upgrade pip uv
9+ # Install uv for fast Python package management
10+ RUN pip install --upgrade pip uv
1611
17- # Create non-root user early
18- RUN addgroup --system --gid 1001 appuser && \
19- adduser --system -- uid 1001 --ingroup appuser appuser
12+ # Create non-root user
13+ RUN groupadd --gid 1001 appuser && \
14+ useradd --uid 1001 --gid appuser --shell /bin/bash --create-home appuser
2015
2116WORKDIR /app
2217
23- # =============================================================================
24- # Dependencies stage: Install and cache Python dependencies
25- # =============================================================================
26- FROM base AS deps
27-
28- # Install build dependencies
29- RUN apk add --no-cache \
30- build-base \
31- linux-headers \
32- git
33-
3418# Copy dependency files first (better layer caching)
35- COPY pyproject.toml ./
19+ COPY pyproject.toml uv.lock* ./
3620
37- # Install dependencies with uv (much faster than pip)
21+ # Install dependencies
3822RUN --mount=type=cache,target=/root/.cache/uv \
39- --mount=type=cache,target=/root/.cache/pip \
40- uv pip install --system '.[dev]' taskipy
41-
42- # =============================================================================
43- # Test stage: Run linting and tests
44- # =============================================================================
45- FROM deps AS test
23+ uv sync --locked --dev
4624
4725# Copy source code
4826COPY . .
@@ -51,103 +29,23 @@ COPY . .
5129RUN chown -R appuser:appuser /app
5230USER appuser
5331
54- # Set build arguments for conditional testing
55- ARG TESTBUILD=true
56- ENV TESTBUILD=$TESTBUILD
57-
58- # Run quality checks and tests if enabled
59- RUN if [ "$TESTBUILD" = "true" ]; then \
60- echo "🔍 Running linting..." && \
61- task lint && \
62- echo "🧪 Running tests..." && \
63- task test && \
64- echo "✅ All quality checks passed!" ; \
65- fi
66-
67- # =============================================================================
68- # Build stage: Create wheel distribution
69- # =============================================================================
70- FROM test AS build
71-
72- # Build wheel package
73- RUN --mount=type=cache,target=/root/.cache/uv \
74- uv build --wheel --out-dir dist
75-
76- # =============================================================================
77- # Security scanning stage (optional but recommended)
78- # =============================================================================
79- FROM aquasec/trivy:latest AS security-scan
80-
81- # Copy built artifacts for scanning
82- COPY --from=build /app/dist /scan/dist
83- COPY --from=build /app/pyproject.toml /scan/
84-
85- # Run security scan (will fail build on HIGH/CRITICAL vulnerabilities)
86- RUN trivy fs --exit-code 1 --severity HIGH,CRITICAL /scan || \
87- (echo "❌ Security vulnerabilities found! Check the output above." && exit 1)
88-
89- # =============================================================================
90- # Runtime preparation: Install wheel in clean Python environment
91- # =============================================================================
92- FROM python:${PYTHON_VERSION}-alpine AS runtime-prep
93-
94- # Install the wheel package in a clean environment
95- COPY --from=build /app/dist/*.whl /tmp/
96- RUN pip install --prefix=/app/python /tmp/*.whl
97-
98- # =============================================================================
99- # Production stage: Minimal distroless runtime
100- # =============================================================================
101- FROM gcr.io/distroless/python3-debian12:latest AS production
102-
103- # Copy installed Python packages from runtime prep
104- COPY --from=runtime-prep /app/python /usr/local
105-
106- # Set working directory
107- WORKDIR /app
108-
109- # Use non-root user (distroless default nonroot user)
110- USER nonroot:nonroot
111-
112- # Configure Python for production
32+ # Configure Python
33+ ENV PYTHONPATH=/app
11334ENV PYTHONUNBUFFERED=1
11435ENV PYTHONDONTWRITEBYTECODE=1
115- ENV PYTHONHASHSEED=random
11636
117- # Health check using module execution
37+ # Expose common ports
38+ EXPOSE 8000 8080 5678
39+
40+ # Health check
11841HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
11942 CMD python -m python_package_template.python_module_template || exit 1
12043
12144# Default command
12245CMD ["python" , "-m" , "python_package_template.python_module_template" ]
12346
124- # =============================================================================
125- # Development stage: For local development with hot reload
126- # =============================================================================
127- FROM deps AS development
128-
129- # Install development tools
130- RUN --mount=type=cache,target=/root/.cache/uv \
131- uv pip install --system watchdog
132-
133- # Copy source code
134- COPY . .
135-
136- # Change ownership and switch to non-root user
137- RUN chown -R appuser:appuser /app
138- USER appuser
139-
140- # Expose common development ports
141- EXPOSE 8000 8080 5678
142-
143- # Development command with auto-reload
144- CMD ["python" , "-m" , "python_package_template.python_module_template" ]
145-
146- # =============================================================================
147- # Metadata and labels
148- # =============================================================================
47+ # Labels
14948LABEL maintainer="eol"
150- LABEL version="0.1.20260411"
151- LABEL description="Python project template with modern Docker practices"
152- LABEL org.opencontainers.image.source="https://github.com/nullhack/python-project-template"
153- LABEL org.opencontainers.image.documentation="https://github.com/nullhack/python-project-template/tree/main/docs/api/"
49+ LABEL version="2.0.20260411"
50+ LABEL description="Python project template - simplified Docker setup"
51+ LABEL org.opencontainers.image.source="https://github.com/nullhack/python-project-template"
0 commit comments