forked from username/flaskpaste
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:
@@ -1,18 +1,17 @@
|
||||
# FlaskPaste Slim Container Image (Distroless)
|
||||
# FlaskPaste Slim Container Image (Alpine)
|
||||
# Build: podman build -f Containerfile.slim -t 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
|
||||
# For debugging, use the standard Containerfile instead
|
||||
# Alpine: Minimal base image with musl libc, actively maintained security patches
|
||||
# For debugging with more tools, use the standard Containerfile instead
|
||||
|
||||
# Stage 1: Build dependencies (identical to standard build)
|
||||
FROM python:3.11-slim AS builder
|
||||
# Stage 1: Build dependencies
|
||||
FROM python:3.11-alpine AS builder
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
# Install build dependencies
|
||||
RUN apt update && apt install -y --no-install-recommends gcc \
|
||||
&& apt clean && rm -rf /var/lib/apt/lists/*
|
||||
# Install build dependencies for compiled Python packages
|
||||
RUN apk add --no-cache gcc musl-dev libffi-dev
|
||||
|
||||
# Create virtual environment and upgrade pip
|
||||
RUN python -m venv /opt/venv
|
||||
@@ -21,29 +20,35 @@ RUN pip install --no-cache-dir --upgrade pip wheel
|
||||
|
||||
# Install Python dependencies
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt gunicorn \
|
||||
&& rm -rf /opt/venv/lib/python*/site-packages/setuptools/_vendor/jaraco.context*.dist-info
|
||||
RUN pip install --no-cache-dir -r requirements.txt gunicorn
|
||||
|
||||
|
||||
# Stage 2: Distroless runtime
|
||||
FROM gcr.io/distroless/python3-debian12:nonroot
|
||||
# Stage 2: Alpine runtime (minimal)
|
||||
FROM python:3.11-alpine
|
||||
|
||||
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
|
||||
|
||||
# Copy virtual environment from builder
|
||||
# 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 application files
|
||||
COPY --chown=65532:65532 app/ ./app/
|
||||
COPY --chown=65532:65532 wsgi.py .
|
||||
COPY --chown=65532:65532 fpaste .
|
||||
|
||||
# Create data directory
|
||||
RUN mkdir -p /app/data && chown -R flaskpaste:flaskpaste /app
|
||||
|
||||
USER flaskpaste
|
||||
|
||||
# Environment configuration
|
||||
ENV PYTHONPATH="/app/.local/lib/python3.11/site-packages"
|
||||
ENV FLASK_ENV=production
|
||||
ENV FLASKPASTE_DB=/app/data/pastes.db
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
@@ -51,10 +56,7 @@ ENV PYTHONDONTWRITEBYTECODE=1
|
||||
|
||||
EXPOSE 5000
|
||||
|
||||
# Healthcheck without shell (no || exit 1)
|
||||
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
|
||||
ENTRYPOINT ["python", "-m", "gunicorn"]
|
||||
CMD ["--bind", "0.0.0.0:5000", "--workers", "2", "--access-logfile", "-", "wsgi:application"]
|
||||
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "--access-logfile", "-", "wsgi:application"]
|
||||
|
||||
Reference in New Issue
Block a user