Files
StarPunk/Containerfile
Phil Skentelbery c559f89a7f feat: add production container support with health check endpoint
Implements Phase 5 containerization specification:
- Add /health endpoint for container monitoring
- Create multi-stage Containerfile (Podman/Docker compatible)
- Add compose.yaml for orchestration
- Add Caddyfile.example for reverse proxy (auto-HTTPS)
- Add nginx.conf.example as alternative
- Update .env.example with container and RSS feed variables
- Add gunicorn WSGI server to requirements.txt

Container features:
- Multi-stage build for smaller image size
- Non-root user (starpunk:1000)
- Health check with database connectivity test
- Volume mount for data persistence
- Resource limits and logging configuration
- Security headers and HTTPS configuration examples

Health check endpoint:
- Tests database connectivity
- Verifies filesystem access
- Returns JSON with status, version, and environment

Following Phase 5 design in docs/designs/phase-5-rss-and-container.md
2025-11-19 10:02:41 -07:00

84 lines
2.6 KiB
Docker

# syntax=docker/dockerfile:1
# Multi-stage build for StarPunk production container
# Podman and Docker compatible
# ============================================================================
# Build Stage - Install dependencies in virtual environment
# ============================================================================
FROM python:3.11-slim AS builder
# Install uv for fast dependency installation
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
WORKDIR /build
# Copy dependency files
COPY requirements.txt .
# Create virtual environment and install dependencies
# Using uv for fast, reproducible installs
RUN uv venv /opt/venv && \
. /opt/venv/bin/activate && \
uv pip install --no-cache -r requirements.txt
# ============================================================================
# Runtime Stage - Minimal production image
# ============================================================================
FROM python:3.11-slim
# Create non-root user for security
# UID/GID 1000 is standard for first user on most systems
RUN useradd --uid 1000 --create-home --shell /bin/bash starpunk && \
mkdir -p /app /data/notes && \
chown -R starpunk:starpunk /app /data
# Copy virtual environment from builder stage
COPY --from=builder /opt/venv /opt/venv
# Set environment variables
ENV PATH="/opt/venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
FLASK_APP=app.py \
DATA_PATH=/data \
NOTES_PATH=/data/notes \
DATABASE_PATH=/data/starpunk.db
WORKDIR /app
# Copy application code
COPY --chown=starpunk:starpunk . .
# Switch to non-root user
USER starpunk
# Expose application port
EXPOSE 8000
# Health check
# Uses httpx (already in requirements) to verify app is responding
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD python3 -c "import httpx; httpx.get('http://localhost:8000/health', timeout=2.0)" || exit 1
# Run gunicorn WSGI server
# - 4 workers for concurrency (adjust based on CPU cores)
# - Sync worker class (simple, reliable)
# - Worker tmp dir in /dev/shm (shared memory, faster)
# - Worker recycling to prevent memory leaks
# - 30s timeout for slow requests
# - Log to stdout/stderr for container log collection
CMD ["gunicorn", \
"--bind", "0.0.0.0:8000", \
"--workers", "4", \
"--worker-class", "sync", \
"--worker-tmp-dir", "/dev/shm", \
"--max-requests", "1000", \
"--max-requests-jitter", "50", \
"--timeout", "30", \
"--graceful-timeout", "30", \
"--access-logfile", "-", \
"--error-logfile", "-", \
"--log-level", "info", \
"app:app"]