forked from username/flaskpaste
71 lines
1.9 KiB
Docker
71 lines
1.9 KiB
Docker
# FlaskPaste Container Image (Multi-Stage Build)
|
|
# Build: podman build -t flaskpaste .
|
|
# Run: podman run -d -p 5000:5000 -v flaskpaste-data:/app/data flaskpaste
|
|
|
|
# Stage 1: Build dependencies
|
|
FROM python:3.11-slim AS builder
|
|
|
|
WORKDIR /build
|
|
|
|
# Install build dependencies and clean up
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
gcc \
|
|
&& apt-get clean \
|
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
|
|
|
# Create virtual environment
|
|
RUN python -m venv /opt/venv
|
|
ENV PATH="/opt/venv/bin:$PATH"
|
|
|
|
# Upgrade pip/setuptools first (pulls in security-fixed jaraco.context)
|
|
RUN pip install --no-cache-dir --upgrade pip setuptools wheel
|
|
|
|
# Install Python dependencies
|
|
COPY requirements.txt .
|
|
RUN pip install --no-cache-dir -r requirements.txt gunicorn
|
|
|
|
|
|
# Stage 2: Runtime image
|
|
FROM python:3.11-slim
|
|
|
|
LABEL maintainer="FlaskPaste"
|
|
LABEL description="Lightweight secure pastebin REST API"
|
|
|
|
# Clean base image caches and create non-root user
|
|
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
|
|
&& groupadd -r flaskpaste && useradd -r -g flaskpaste flaskpaste
|
|
|
|
# Copy virtual environment from builder
|
|
COPY --from=builder /opt/venv /opt/venv
|
|
ENV PATH="/opt/venv/bin:$PATH"
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy only necessary application files
|
|
COPY app/ ./app/
|
|
COPY wsgi.py .
|
|
COPY fpaste .
|
|
|
|
# Create data directory with correct ownership
|
|
RUN mkdir -p /app/data && chown -R flaskpaste:flaskpaste /app
|
|
|
|
# Switch to non-root user
|
|
USER flaskpaste
|
|
|
|
# Environment defaults
|
|
ENV FLASK_ENV=production
|
|
ENV FLASKPASTE_DB=/app/data/pastes.db
|
|
ENV PYTHONUNBUFFERED=1
|
|
ENV PYTHONDONTWRITEBYTECODE=1
|
|
|
|
# Expose port
|
|
EXPOSE 5000
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
|
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')" || exit 1
|
|
|
|
# Run with gunicorn
|
|
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "--access-logfile", "-", "wsgi:application"]
|