Production entrypoint switch:
- pyproject.toml: add gunicorn>=23.0 dep
- gunicorn.conf.py: env-var-driven config (bind, workers, threads,
timeout, graceful_timeout, keepalive, log level, access-log format)
- Dockerfile: CMD gunicorn main:app (auto-loads gunicorn.conf.py from
WORKDIR /app)
- docker-compose.yml: forward GUNICORN_WORKERS / GUNICORN_TIMEOUT /
GUNICORN_BIND
- .env.example: document the new tunables
Why gunicorn over standalone uvicorn for production:
- Process supervision: master restarts crashed workers, restarts on
memory leaks
- Graceful shutdown: SIGTERM drains workers in-flight
- Multi-worker: concurrent requests actually run in parallel
- Standard ops: k8s readiness probes, log aggregators, etc. all know
gunicorn
Why uvicorn workers (not sync workers): gunicorn can't natively serve
ASGI; uvicorn.workers.UvicornWorker is the canonical way to run an
ASGI app under gunicorn.
Defaults:
- 2 workers (small MCP service; raise for high concurrency)
- 1 thread per worker (no blocking I/O)
- 120s timeout (yarn logs can be slow; uvicorn's 30s default is too
tight)
Verified: gunicorn boots, lifespan runs (14 tools logged), MCP
initialize + tools/list + tools/call all work, /openapi.json = 200,
multiple gunicorn worker processes visible in ps.
uv sync picked up gunicorn 26.0.0. Tests still 116/116.
75 lines
2.5 KiB
Docker
75 lines
2.5 KiB
Docker
# syntax=docker/dockerfile:1
|
|
#
|
|
# Spark Executor MCP — runtime image.
|
|
# Build deps with uv (frozen, prod-only), then drop in the source on top of
|
|
# ppython:3.12-slim-bookworm with Spark + YARN configs mounted for the spark-submit /
|
|
# yarn CLI calls inside the MCP tools.
|
|
|
|
FROM python:3.12-slim-bookworm
|
|
|
|
# --- uv (official binary) ---
|
|
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /usr/local/bin/
|
|
|
|
# --- Spark + Hadoop config (matches the original Dockerfile) ---
|
|
|
|
ARG SPARK_VERSION=4.1.2
|
|
|
|
RUN sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources && \
|
|
apt-get update && \
|
|
apt-get install -y --no-install-recommends \
|
|
curl \
|
|
ca-certificates \
|
|
tar \
|
|
openjdk-17-jre-headless && \
|
|
curl -L \
|
|
https://mirrors.tuna.tsinghua.edu.cn/apache/spark/spark-${SPARK_VERSION}/spark-${SPARK_VERSION}-bin-hadoop3.tgz \
|
|
-o /tmp/spark.tgz && \
|
|
mkdir -p /opt && \
|
|
tar -xzf /tmp/spark.tgz -C /opt && \
|
|
mv /opt/spark-${SPARK_VERSION}-bin-hadoop3 /opt/spark && \
|
|
rm -f /tmp/spark.tgz && \
|
|
apt-get clean && \
|
|
rm -rf /var/lib/apt/lists/*
|
|
|
|
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
|
|
ENV SPARK_HOME=/opt/spark
|
|
ENV PATH=${JAVA_HOME}/bin:${SPARK_HOME}/bin:${PATH}
|
|
|
|
# Hadoop/Yarn 配置目录(运行时挂载)
|
|
RUN mkdir -p /etc/hadoop/conf
|
|
|
|
# 默认值,可在 docker run 时覆盖
|
|
ENV HADOOP_CONF_DIR=/etc/hadoop/conf
|
|
ENV YARN_CONF_DIR=/etc/hadoop/conf
|
|
|
|
# --- App ---
|
|
WORKDIR /app
|
|
|
|
# Install Python deps first so this layer caches independently of source.
|
|
# --frozen pins to uv.lock exactly; --no-dev skips pytest etc. for a slim
|
|
# production image; --no-install-project defers copying the source.
|
|
COPY pyproject.toml uv.lock ./
|
|
RUN uv sync --index-url=https://pypi.tuna.tsinghua.edu.cn/simple/ --frozen --no-dev --no-install-project
|
|
|
|
# Now copy the source and let uv wire it in.
|
|
COPY main.py ./
|
|
COPY spark_executor ./spark_executor
|
|
COPY common ./common
|
|
COPY gunicorn.conf.py ./
|
|
RUN uv sync --index-url=https://pypi.tuna.tsinghua.edu.cn/simple/ --frozen --no-dev
|
|
|
|
# Put the venv on PATH so `python` / `gunicorn` / `uvicorn` resolve to the project env.
|
|
ENV PATH=/app/.venv/bin:$PATH
|
|
ENV PYTHONUNBUFFERED=1
|
|
|
|
# gunicorn is the prod entrypoint — multiple ASGI workers, graceful
|
|
# shutdown, stdout/stderr logs. Config knobs are env-var driven (see
|
|
# gunicorn.conf.py).
|
|
#
|
|
# Common overrides via -e flags at `docker run`:
|
|
# -e GUNICORN_WORKERS=4
|
|
# -e GUNICORN_TIMEOUT=180
|
|
# -e GUNICORN_BIND=0.0.0.0:9000
|
|
EXPOSE 8000
|
|
CMD ["gunicorn", "main:app"]
|