saas_api/Dockerfile

66 lines
2 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ==============================
# STAGE 1 Build das dependências
# ==============================
FROM python:3.12-slim AS builder
WORKDIR /app
# Copia apenas requirements para cache eficiente
COPY requirements.txt .
# Instala dependências necessárias apenas para build
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc libffi-dev libssl-dev python3-dev firebird-dev build-essential curl nano \
&& pip install --upgrade pip \
&& pip install --no-cache-dir --prefix=/install -r requirements.txt \
&& apt-get purge -y gcc build-essential python3-dev \
&& apt-get autoremove -y && rm -rf /var/lib/apt/lists/*
# ==============================
# STAGE 2 Runtime (produção)
# ==============================
FROM python:3.12-slim AS runtime
# Define diretório de trabalho
WORKDIR /app
# Copia dependências instaladas do estágio builder
COPY --from=builder /install /usr/local
# Copia todo o código do projeto
COPY . .
# Define timezone e variáveis de ambiente padrão
ENV TZ=America/Sao_Paulo \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
UVICORN_WORKERS=4 \
UVICORN_PORT=8000 \
UVICORN_HOST=0.0.0.0 \
GUNICORN_CMD_ARGS="--log-level info --timeout 60 --graceful-timeout 30 --keep-alive 10"
# Instala utilitários para debug e health checks
RUN apt-get update && apt-get install -y --no-install-recommends curl nano \
&& rm -rf /var/lib/apt/lists/*
# Expõe a porta padrão do app
EXPOSE 8000
# ==============================
# HEALTHCHECK automático
# ==============================
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# ==============================
# Comando de inicialização
# (Gunicorn + Uvicorn Workers)
# ==============================
CMD ["sh", "-c", "\
gunicorn main:app \
--worker-class uvicorn.workers.UvicornWorker \
--workers ${UVICORN_WORKERS:-4} \
--bind ${UVICORN_HOST:-0.0.0.0}:${UVICORN_PORT:-8000} \
--access-logfile '-' \
--error-logfile '-' \
"]