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
|
# 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
|
FROM python:3.11-slim-bookworm AS builder
|
||||||
|
|
||||||
|
|
@ -10,15 +10,19 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
PIP_NO_CACHE_DIR=1 \
|
PIP_NO_CACHE_DIR=1 \
|
||||||
PIP_DISABLE_PIP_VERSION_CHECK=1
|
PIP_DISABLE_PIP_VERSION_CHECK=1
|
||||||
|
|
||||||
# Install runtime deps into /install. --target keeps everything in one tree
|
# --copies bundles the Python binary itself into /opt/venv so the runtime
|
||||||
# so we can copy it cleanly to a distroless image without venv/shebang issues.
|
# stage gets a self-contained tree (no symlinks back to /usr/local/bin).
|
||||||
RUN pip install --target=/install \
|
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.* \
|
fastapi==0.115.* \
|
||||||
"uvicorn[standard]==0.30.*" \
|
"uvicorn[standard]==0.30.*" \
|
||||||
redis==5.0.*
|
redis==5.0.*
|
||||||
|
|
||||||
# Pre-compile bytecode for faster cold start under a read-only rootfs
|
# Pre-compile bytecode for faster cold start on a read-only rootfs
|
||||||
RUN python -m compileall -q /install
|
RUN python -m compileall -q /opt/venv
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# Runtime stage: distroless Python 3.11, non-root, no shell
|
# 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
|
FROM gcr.io/distroless/python3-debian12:nonroot
|
||||||
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
PYTHONUNBUFFERED=1 \
|
PYTHONUNBUFFERED=1
|
||||||
PYTHONPATH=/app/lib
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Site-packages first (rarely changes → maximises layer cache)
|
# Self-contained venv (its bin/python and site-packages travel together)
|
||||||
COPY --from=builder --chown=nonroot:nonroot /install /app/lib
|
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/
|
COPY --chown=nonroot:nonroot app.py favicon.ico /app/
|
||||||
|
|
||||||
USER nonroot
|
USER nonroot
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
# Module form (-m uvicorn) sidesteps broken shebangs in /app/lib/bin/uvicorn
|
# Invoking the venv's Python directly makes its site-packages discoverable
|
||||||
# (those were generated against the builder's Python path, not distroless's).
|
# automatically (sys.prefix points inside /opt/venv) — no PYTHONPATH needed.
|
||||||
ENTRYPOINT ["/usr/bin/python3", "-m", "uvicorn"]
|
ENTRYPOINT ["/opt/venv/bin/python", "-m", "uvicorn"]
|
||||||
CMD ["app:app", "--host", "0.0.0.0", "--port", "8000"]
|
CMD ["app:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||||
|
|
|
||||||
4
app.py
4
app.py
|
|
@ -68,8 +68,8 @@ def init_valkey():
|
||||||
redis_client = redis.from_url(VALKEY_URL, decode_responses=True)
|
redis_client = redis.from_url(VALKEY_URL, decode_responses=True)
|
||||||
redis_client.ping() # Test connection
|
redis_client.ping() # Test connection
|
||||||
print(f"Valkey/Redis connected: {VALKEY_URL}")
|
print(f"Valkey/Redis connected: {VALKEY_URL}")
|
||||||
except ImportError:
|
except ImportError as e:
|
||||||
print("Warning: redis package not installed, falling back to memory-only storage")
|
print(f"Warning: redis package import failed ({e}), falling back to memory-only storage")
|
||||||
redis_client = None
|
redis_client = None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: Failed to connect to Valkey/Redis: {e}")
|
print(f"Warning: Failed to connect to Valkey/Redis: {e}")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue