docker --version
Docker Hub, ACR, or GCR
Build optimized Docker images, push to a registry, scan for vulnerabilities, and automate via CI.
1 # ─── Build stage ────────────────────────────────────────────────── 2 FROM python:3.11.9-slim AS builder 3 4 WORKDIR /build 5 6 # Install build dependencies separately for better caching 7 COPY requirements.txt . 8 RUN pip install --no-cache-dir --user -r requirements.txt 9 10 # ─── Runtime stage ──────────────────────────────────────────────── 11 FROM python:3.11.9-slim AS runtime 12 13 # Non-root user for security 14 RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app 15 16 WORKDIR /app 17 18 # Copy only installed packages from builder 19 COPY --from=builder /root/.local /home/app/.local 20 COPY --chown=app:app src/ . 21 22 USER app 23 24 ENV PATH=/home/app/.local/bin:$PATH PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 25 26 HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8080/health')" 27 28 EXPOSE 8080 29 30 CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"] 31
Sign in to share your feedback and join the discussion.