deply: FIX redis problem with distroless deployment
This commit is contained in:
parent
fc1be4e278
commit
27f61aa94f
2 changed files with 19 additions and 16 deletions
31
Dockerfile
31
Dockerfile
|
|
@ -1,7 +1,7 @@
|
|||
# syntax=docker/dockerfile:1.7
|
||||
|
||||
# ============================================================================
|
||||
# Build stage: install Python dependencies into a dedicated prefix
|
||||
# Build stage: create a self-contained venv with all dependencies
|
||||
# ============================================================================
|
||||
FROM python:3.11-slim-bookworm AS builder
|
||||
|
||||
|
|
@ -10,15 +10,19 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
|
|||
PIP_NO_CACHE_DIR=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=1
|
||||
|
||||
# Install runtime deps into /install. --target keeps everything in one tree
|
||||
# so we can copy it cleanly to a distroless image without venv/shebang issues.
|
||||
RUN pip install --target=/install \
|
||||
# --copies bundles the Python binary itself into /opt/venv so the runtime
|
||||
# stage gets a self-contained tree (no symlinks back to /usr/local/bin).
|
||||
RUN python -m venv --copies /opt/venv
|
||||
ENV PATH=/opt/venv/bin:$PATH
|
||||
|
||||
# Install runtime deps into the venv's site-packages
|
||||
RUN pip install \
|
||||
fastapi==0.115.* \
|
||||
"uvicorn[standard]==0.30.*" \
|
||||
redis==5.0.*
|
||||
|
||||
# Pre-compile bytecode for faster cold start under a read-only rootfs
|
||||
RUN python -m compileall -q /install
|
||||
# Pre-compile bytecode for faster cold start on a read-only rootfs
|
||||
RUN python -m compileall -q /opt/venv
|
||||
|
||||
# ============================================================================
|
||||
# Runtime stage: distroless Python 3.11, non-root, no shell
|
||||
|
|
@ -29,21 +33,20 @@ RUN python -m compileall -q /install
|
|||
FROM gcr.io/distroless/python3-debian12:nonroot
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PYTHONPATH=/app/lib
|
||||
PYTHONUNBUFFERED=1
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Site-packages first (rarely changes → maximises layer cache)
|
||||
COPY --from=builder --chown=nonroot:nonroot /install /app/lib
|
||||
# Self-contained venv (its bin/python and site-packages travel together)
|
||||
COPY --from=builder --chown=nonroot:nonroot /opt/venv /opt/venv
|
||||
|
||||
# Application source (changes most often → its own layer)
|
||||
# Application source (its own layer so source-only changes don't bust dep cache)
|
||||
COPY --chown=nonroot:nonroot app.py favicon.ico /app/
|
||||
|
||||
USER nonroot
|
||||
EXPOSE 8000
|
||||
|
||||
# Module form (-m uvicorn) sidesteps broken shebangs in /app/lib/bin/uvicorn
|
||||
# (those were generated against the builder's Python path, not distroless's).
|
||||
ENTRYPOINT ["/usr/bin/python3", "-m", "uvicorn"]
|
||||
# Invoking the venv's Python directly makes its site-packages discoverable
|
||||
# automatically (sys.prefix points inside /opt/venv) — no PYTHONPATH needed.
|
||||
ENTRYPOINT ["/opt/venv/bin/python", "-m", "uvicorn"]
|
||||
CMD ["app:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue