الملفات
ghaymah-exam-marwan-abdelmo…/q1-deploy-monitor/Dockerfile

65 أسطر
2.0 KiB
Docker

# ==============================================================================
# Ghaymah Cloud SRE Exam — Q1 Production Dockerfile
# Multi-Stage Build with Non-Root Security Context & Built-in Healthcheck
# ==============================================================================
# Stage 1: Build & Dependencies
FROM python:3.11-slim AS builder
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential curl && \
rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir fastapi uvicorn pydantic requests
# Stage 2: Production Runtime
FROM python:3.11-slim AS runner
# Non-root user creation for security compliance
RUN groupadd -g 10001 appgroup && \
useradd -u 10001 -g appgroup -s /bin/sh -m appuser
WORKDIR /app
COPY --from=builder /usr/local /usr/local
# Inline FastAPI Application Code
RUN echo 'import time, os\n\
from fastapi import FastAPI, Response\n\
app = FastAPI(title="Ghaymah SRE Q1 API")\n\
START_TIME = time.time()\n\
REQ_COUNT = 0\n\
\n\
@app.middleware("http")\n\
async def count_req(request, call_next):\n\
global REQ_COUNT\n\
REQ_COUNT += 1\n\
return await call_next(request)\n\
\n\
@app.get("/")\n\
def root(): return {"status": "online", "system": "ghaymah.systems"}\n\
\n\
@app.get("/healthz")\n\
@app.get("/health")\n\
def health():\n\
return {"status": "healthy", "code": 200, "uptime": round(time.time()-START_TIME,2), "requests": REQ_COUNT}\n\
\n\
@app.get("/livez")\n\
def live(): return {"status": "alive"}\n\
\n\
@app.get("/readyz")\n\
def ready(): return {"status": "ready"}\n\
' > /app/main.py
RUN chown -R appuser:appgroup /app
USER appuser
EXPOSE 8000
# Docker Container Healthcheck
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/healthz')" || exit 1
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]