containerfile: switch slim image to alpine base

Debian distroless had 5 critical CVEs (unfixed in Debian 12).
Alpine has active security patches and smaller footprint.
This commit is contained in:
Username
2026-01-19 23:58:34 +01:00
parent 54190487c8
commit 0f5742ccc2

View File

@@ -1,18 +1,17 @@
# FlaskPaste Slim Container Image (Distroless) # FlaskPaste Slim Container Image (Alpine)
# Build: podman build -f Containerfile.slim -t flaskpaste:slim . # Build: podman build -f Containerfile.slim -t flaskpaste:slim .
# Run: podman run -d -p 5000:5000 -v flaskpaste-data:/app/data flaskpaste:slim # Run: podman run -d -p 5000:5000 -v flaskpaste-data:/app/data flaskpaste:slim
# #
# Distroless: No shell, no package manager, minimal attack surface # Alpine: Minimal base image with musl libc, actively maintained security patches
# For debugging, use the standard Containerfile instead # For debugging with more tools, use the standard Containerfile instead
# Stage 1: Build dependencies (identical to standard build) # Stage 1: Build dependencies
FROM python:3.11-slim AS builder FROM python:3.11-alpine AS builder
WORKDIR /build WORKDIR /build
# Install build dependencies # Install build dependencies for compiled Python packages
RUN apt update && apt install -y --no-install-recommends gcc \ RUN apk add --no-cache gcc musl-dev libffi-dev
&& apt clean && rm -rf /var/lib/apt/lists/*
# Create virtual environment and upgrade pip # Create virtual environment and upgrade pip
RUN python -m venv /opt/venv RUN python -m venv /opt/venv
@@ -21,29 +20,35 @@ RUN pip install --no-cache-dir --upgrade pip wheel
# Install Python dependencies # Install Python dependencies
COPY requirements.txt . COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt gunicorn \ RUN pip install --no-cache-dir -r requirements.txt gunicorn
&& rm -rf /opt/venv/lib/python*/site-packages/setuptools/_vendor/jaraco.context*.dist-info
# Stage 2: Distroless runtime # Stage 2: Alpine runtime (minimal)
FROM gcr.io/distroless/python3-debian12:nonroot FROM python:3.11-alpine
LABEL maintainer="FlaskPaste" LABEL maintainer="FlaskPaste"
LABEL description="Minimal secure pastebin REST API (distroless)" LABEL description="Minimal secure pastebin REST API (Alpine)"
# Create non-root user
RUN addgroup -g 65532 -S flaskpaste && adduser -u 65532 -S -G flaskpaste flaskpaste
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /app WORKDIR /app
# Copy virtual environment from builder # Copy application files
# Note: distroless uses /usr/lib/python3.11 as base, venv provides all deps
COPY --from=builder /opt/venv/lib/python3.11/site-packages /app/.local/lib/python3.11/site-packages
# Copy application files (owned by nonroot user - uid 65532)
COPY --chown=65532:65532 app/ ./app/ COPY --chown=65532:65532 app/ ./app/
COPY --chown=65532:65532 wsgi.py . COPY --chown=65532:65532 wsgi.py .
COPY --chown=65532:65532 fpaste . COPY --chown=65532:65532 fpaste .
# Create data directory
RUN mkdir -p /app/data && chown -R flaskpaste:flaskpaste /app
USER flaskpaste
# Environment configuration # Environment configuration
ENV PYTHONPATH="/app/.local/lib/python3.11/site-packages"
ENV FLASK_ENV=production ENV FLASK_ENV=production
ENV FLASKPASTE_DB=/app/data/pastes.db ENV FLASKPASTE_DB=/app/data/pastes.db
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
@@ -51,10 +56,7 @@ ENV PYTHONDONTWRITEBYTECODE=1
EXPOSE 5000 EXPOSE 5000
# Healthcheck without shell (no || exit 1)
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \ HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"] CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')" || exit 1
# Run gunicorn directly via Python module CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "--access-logfile", "-", "wsgi:application"]
ENTRYPOINT ["python", "-m", "gunicorn"]
CMD ["--bind", "0.0.0.0:5000", "--workers", "2", "--access-logfile", "-", "wsgi:application"]