以下 Dockerfile 构建了一个在 uvicorn 的单个实例下运行的工作 Fastapi 演示应用程序:
# example of a multistage build
# Stage 1: Builder
# Use the official larger Docker Python image
FROM python:3.11-bookworm as builder
# Install python modules with known release
RUN pip install poetry==1.8.2
RUN pip install gunicorn==21.2.0
# Set Poetry environment variables for non-interactive installation
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache
WORKDIR /app
# Copy the project definition files
COPY pyproject.toml poetry.lock README.md ./
# Install dependencies without the dev dependencies and without the project package itself
RUN poetry install --no-root --no-dev && rm -rf $POETRY_CACHE_DIR
# Stage 2: Runtime
# now switch to the slimmer version and don't need poetry anymore
FROM python:3.11-slim-bookworm as runtime
# Copy the virtual environment from the builder stage
ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
COPY --from=builder /app/.venv /app/.venv
# configurations, including the current working directory set by WORKDIR, do not persist from one stage to another.
# Set the working directory in the container
WORKDIR /app
# Copy the application code and configuration into the container
COPY src ./src
COPY config ./config
# Documentation of the port the app runs on
EXPOSE 8000
# Define the entrypoint for running the application
ENTRYPOINT ["python", "-m", "uvicorn", "--host", "0.0.0.0", "src.mylib.mymod:app"]
正如您从大量评论的文件中看到的那样,我正在使用多阶段构建,使用 3.11 映像来构建环境,然后使用 slim 映像来复制源、重建环境并运行应用程序。
现在我尝试将最后一行更改为:
ENTRYPOINT ["python", "-m", "gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "src.mylib.mymod:app"]
但出现以下错误:
(.venv) bob /Volumes/2TBWDB/code/uvitest [main] $ docker compose up -d
[+] Running 0/1
⠹ Container uvitest-uvitest-1 Starting 0.2s
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "gunicorn": executable file not found in $PATH: unknown
请帮忙修复。谢谢
问题是您没有将
gunicorn
包复制到第二个 runtime
阶段。
在第一阶段
builder
:gunicorn
得到
/usr/local/lib/python3.11/gunicorn
(+依赖项)/usr/local/bin/gunicorn
两者都在你的第二阶段缺失。
最简单的解决方案是在第二阶段安装缺少的软件包:
RUN pip install --no-cache-dir gunicorn
FROM python:3.11-slim-bookworm as runtime
RUN pip install --no-cache-dir gunicorn
# Copy the virtual environment from the builder stage
ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
COPY --from=builder /app/.venv /app/.venv
# configurations, including the current working directory set by WORKDIR, do not persist from one stage to another.
# Set the working directory in the container
WORKDIR /app
# Copy the application code and configuration into the container
COPY src ./src
COPY config ./config
# Documentation of the port the app runs on
EXPOSE 8000
# Define the entrypoint for running the application
ENTRYPOINT ["python", "-m", "uvicorn", "--host", "0.0.0.0", "src.mylib.mymod:app"]