mitlist/be/Dockerfile.prod
mohamad 392a2ae049
Some checks failed
Deploy to Production, build images and push to Gitea Registry / build_and_push (pull_request) Failing after 19s
refactor: Switch backend Dockerfile to use Alpine package manager
2025-06-01 15:44:09 +02:00

63 lines
1.4 KiB
Docker

# Multi-stage build for production
FROM python:alpine as base
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Install system dependencies
# Use apk for Alpine Linux instead of apt-get
RUN apk add --no-cache \
gcc \
build-essential \
libpq-dev \
curl
# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser appuser
# Development stage
FROM base as development
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN chown -R appuser:appuser /app
USER appuser
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
# Production stage
FROM base as production
WORKDIR /app
# Install production dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Create necessary directories and set permissions
RUN mkdir -p /app/logs && \
chown -R appuser:appuser /app
# Switch to non-root user
USER appuser
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Expose port
EXPOSE 8000
# Production command with optimizations
CMD ["uvicorn", "app.main:app", \
"--host", "0.0.0.0", \
"--port", "8000", \
"--workers", "8", \
"--access-log", \
"--log-level", "info"]