dockerize app

This commit is contained in:
Matteo Rosati
2026-01-20 10:46:14 +01:00
parent eb627047e5
commit 7df1b9f718
5 changed files with 437 additions and 0 deletions

82
Dockerfile Normal file
View File

@@ -0,0 +1,82 @@
# ============================================
# Stage 1: Builder
# ============================================
FROM python:3.13-alpine AS builder
# Install build dependencies
RUN apk add --no-cache \
gcc \
musl-dev \
libffi-dev \
openssl-dev \
cargo
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Set working directory
WORKDIR /build
# Copy requirements first for better caching
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# ============================================
# Stage 2: Runtime
# ============================================
FROM python:3.13-alpine AS runtime
# Install runtime dependencies only
RUN apk add --no-cache \
libstdc++ \
ca-certificates
# Create non-root user for security
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Create application directory
RUN mkdir /app && \
chown -R appuser:appuser /app
WORKDIR /app
# Copy application files
COPY --chown=appuser:appuser app.py .
COPY --chown=appuser:appuser main.py .
COPY --chown=appuser:appuser static ./static
# Copy and setup entrypoint script
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
# Switch to non-root user
USER appuser
# Expose default port (can be overridden via PORT env var)
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:${PORT:-8000}/ || exit 1
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PORT=8000 \
HOST=0.0.0.0 \
WORKERS=1 \
LOG_LEVEL=info
# Use entrypoint script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]