我想构建一个多阶段构建的 Docker 镜像。该图像基于 FastAPI 和 Uvicorn。
如果我使用以下
Dockerfile
进行多阶段构建
# Stage 1: Build stage
FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime as builder
# set working directory
WORKDIR /usr/src
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 python3-pip git \
&& ln -sf python3 /usr/bin/python \
&& ln -sf pip3 /usr/bin/pip \
&& pip install --upgrade pip
# install python dependencies
RUN pip install --upgrade pip setuptools wheel
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN apt-get remove -y git
# Stage 2: Final stage
FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 \
&& ln -sf python3 /usr/bin/python
# Copy the installed dependencies from the previous stage
COPY --from=builder /usr/local /usr/local
ENV PATH=/usr/local/bin:$PATH
WORKDIR /usr/src
# Copy your app code
COPY ./app/ ./app/
并使用以下命令运行它
docker-compose.yml
version: '3.8'
services:
scraiber_gpt:
build:
context: .
dockerfile: Dockerfile
secrets:
- githubpat
command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000
volumes:
- ./src:/usr/src
ports:
- '8000:8000'
我明白了
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: "uvicorn": executable file not found in $PATH: unknown
但是,对于使用
Dockerfile
的非多阶段构建
FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime
# set working directory
WORKDIR /usr/src
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 python3-pip git \
&& ln -sf python3 /usr/bin/python \
&& ln -sf pip3 /usr/bin/pip \
&& pip install --upgrade pip
# install python dependencies
RUN pip install --upgrade pip setuptools wheel
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN apt-get remove -y git
# Copy your app code
COPY ./app/ ./app/
事情往往会起作用。因此,我想知道如何使多阶段构建工作。
最好使用 virtualenv 来管理你的依赖项,试试这个:
# Stage 1: Build stage
FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime as builder
# set working directory
WORKDIR /usr/src
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 python3-pip git \
&& ln -sf python3 /usr/bin/python \
&& ln -sf pip3 /usr/bin/pip \
&& pip install --upgrade pip
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# install python dependencies
RUN pip install --upgrade pip setuptools wheel
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN apt-get remove -y git
# Stage 2: Final stage
FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 \
&& ln -sf python3 /usr/bin/python
NV PYTHONUNBUFFERED 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3 \
&& ln -sf python3 /usr/bin/python
COPY --from=builder /opt/venv /opt/venv
WORKDIR /usr/src
# Copy your app code
COPY ./app/ ./app/