Spaces:
Running
Running
Melika Kheirieh
commited on
Commit
·
428fd71
1
Parent(s):
c21b5e2
chore(docker): add ca-certificates, chown app dir; keep non-root; healthcheck
Browse files- Dockerfile +11 -15
Dockerfile
CHANGED
|
@@ -8,19 +8,15 @@ ENV PIP_NO_CACHE_DIR=1 \
|
|
| 8 |
WORKDIR /build
|
| 9 |
|
| 10 |
# If you truly need to compile deps, keep build-essential.
|
| 11 |
-
# If you use psycopg[binary], you can safely drop
|
| 12 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 13 |
build-essential \
|
| 14 |
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
| 16 |
-
# Leverage Docker layer caching
|
| 17 |
COPY requirements.txt .
|
| 18 |
-
|
| 19 |
-
# Build all dependencies as wheel files inside /wheels
|
| 20 |
RUN pip install --upgrade pip && \
|
| 21 |
pip wheel --wheel-dir /wheels -r requirements.txt
|
| 22 |
|
| 23 |
-
|
| 24 |
# ---------- Stage 2: Runtime image ----------
|
| 25 |
FROM python:3.12-slim AS runtime
|
| 26 |
|
|
@@ -31,19 +27,24 @@ ENV PIP_NO_CACHE_DIR=1 \
|
|
| 31 |
|
| 32 |
WORKDIR /app
|
| 33 |
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
RUN useradd -m appuser
|
| 36 |
|
| 37 |
-
#
|
| 38 |
COPY --from=builder /wheels /wheels
|
| 39 |
-
|
| 40 |
-
# Install dependencies from wheels
|
| 41 |
COPY requirements.txt .
|
| 42 |
RUN pip install --no-cache-dir --find-links=/wheels -r requirements.txt && \
|
| 43 |
rm -rf /wheels
|
| 44 |
|
| 45 |
-
#
|
| 46 |
COPY . .
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# ---------- Metadata & Healthcheck ----------
|
| 49 |
LABEL org.opencontainers.image.title="nl2sql-copilot" \
|
|
@@ -52,16 +53,11 @@ LABEL org.opencontainers.image.title="nl2sql-copilot" \
|
|
| 52 |
org.opencontainers.image.authors="melika.kheirieh" \
|
| 53 |
org.opencontainers.image.licenses="MIT"
|
| 54 |
|
| 55 |
-
# Lightweight healthcheck (no curl)
|
| 56 |
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
|
| 57 |
CMD python -c "import urllib.request, sys; \
|
| 58 |
sys.exit(0) if urllib.request.urlopen('http://127.0.0.1:8000/healthz', timeout=2).getcode() == 200 else sys.exit(1)"
|
| 59 |
|
| 60 |
-
# Drop privileges
|
| 61 |
USER appuser
|
| 62 |
-
|
| 63 |
EXPOSE 8000
|
| 64 |
|
| 65 |
-
# Start FastAPI with Uvicorn
|
| 66 |
-
# (UVICORN_WORKERS can be overridden at runtime)
|
| 67 |
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --workers ${UVICORN_WORKERS:-1}"]
|
|
|
|
| 8 |
WORKDIR /build
|
| 9 |
|
| 10 |
# If you truly need to compile deps, keep build-essential.
|
| 11 |
+
# If you use psycopg[binary], you can safely drop build-essential.
|
| 12 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 13 |
build-essential \
|
| 14 |
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
|
|
|
| 16 |
COPY requirements.txt .
|
|
|
|
|
|
|
| 17 |
RUN pip install --upgrade pip && \
|
| 18 |
pip wheel --wheel-dir /wheels -r requirements.txt
|
| 19 |
|
|
|
|
| 20 |
# ---------- Stage 2: Runtime image ----------
|
| 21 |
FROM python:3.12-slim AS runtime
|
| 22 |
|
|
|
|
| 27 |
|
| 28 |
WORKDIR /app
|
| 29 |
|
| 30 |
+
# HTTPS certs for outbound calls (LLM proxies, etc.)
|
| 31 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 32 |
+
ca-certificates \
|
| 33 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 34 |
+
|
| 35 |
+
# Create a non-root user
|
| 36 |
RUN useradd -m appuser
|
| 37 |
|
| 38 |
+
# Wheels
|
| 39 |
COPY --from=builder /wheels /wheels
|
|
|
|
|
|
|
| 40 |
COPY requirements.txt .
|
| 41 |
RUN pip install --no-cache-dir --find-links=/wheels -r requirements.txt && \
|
| 42 |
rm -rf /wheels
|
| 43 |
|
| 44 |
+
# App code
|
| 45 |
COPY . .
|
| 46 |
+
# Allow non-root writes if app writes logs/db files
|
| 47 |
+
RUN chown -R appuser:appuser /app
|
| 48 |
|
| 49 |
# ---------- Metadata & Healthcheck ----------
|
| 50 |
LABEL org.opencontainers.image.title="nl2sql-copilot" \
|
|
|
|
| 53 |
org.opencontainers.image.authors="melika.kheirieh" \
|
| 54 |
org.opencontainers.image.licenses="MIT"
|
| 55 |
|
|
|
|
| 56 |
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
|
| 57 |
CMD python -c "import urllib.request, sys; \
|
| 58 |
sys.exit(0) if urllib.request.urlopen('http://127.0.0.1:8000/healthz', timeout=2).getcode() == 200 else sys.exit(1)"
|
| 59 |
|
|
|
|
| 60 |
USER appuser
|
|
|
|
| 61 |
EXPOSE 8000
|
| 62 |
|
|
|
|
|
|
|
| 63 |
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --workers ${UVICORN_WORKERS:-1}"]
|