诗歌被覆盖在 Docker ,我不明白为什么-你能发现问题吗?

sczxawaw  于 2023-01-20  发布在  Docker
关注(0)|答案(2)|浏览(110)

以下是我尝试在Docker容器中直接运行poetry(在排除故障时我放弃了PATH)后出现的错误:
Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "~/.local/share/pypoetry/venv/bin/poetry": stat ~/.local/share/pypoetry/venv/bin/poetry: no such file or directory: unknown
停靠文件:

FROM python:3.10

# to run poetry directly as soon as it's installed
ENV PATH="/root/.local/bin:$PATH"

# INSTALL POETRY
RUN apt-get update \
    && apt-get install -y curl \
    && curl -sSL https://install.python-poetry.org | python3 - 

# set work directory
WORKDIR /app

# Install dependencies
COPY poetry.lock pyproject.toml /app/
RUN ~/.local/share/pypoetry/venv/bin/poetry install --no-root

# copy files to Container
COPY ./. /app
CMD ["~/.local/share/pypoetry/venv/bin/poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

docker-compose.yml

# Docker compose file for nginx and letsencrypt
version: "3.10"
services:
  fastapi:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: fastapi
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - .:/app

奇怪的是,poetry实际上会先构建和安装所有这些依赖项,我看着它们安装。然后当我尝试在cmd行使用poetry时,你会发现构建错误产生了上面的错误。突然在构建的那个点,poetry安装不见了?

rur96b6h

rur96b6h1#

这里使用的是JSON数组形式的CMD,它不运行shell,也不进行任何类型的变量扩展,特别是,~引用当前用户的主目录是shell提供的便利,但是由于这里没有运行shell,系统将其解释为查找当前目录的子目录,其字面名称为~
最短的解决方案是在这里拼出绝对路径

CMD ["/root/.local/share/pypoetry/venv/bin/poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
#     ^^^^^ explicitly spell out user's home directory

您尝试更早地设置$PATH,这里的另一种方法是将Poetry虚拟环境添加到$PATH

ENV PATH="/root/.local/bin:/root/.local/share/pypoetry/venv/bin:$PATH"
#                      add ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

...

RUN poetry install --no-root
CMD ["poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

看起来可以使用pip安装Poetry,在这种情况下,您可以在虚拟环境之外安装它,或者使用poetry export将项目的依赖项安装到requirements.txt文件,您可以更直接地将RUN pip install -r安装到映像的“系统”Python中。

lmyy7pcs

lmyy7pcs2#

$HOME目录是您设置的工作目录,因此当您执行RUN ~/.local/share/pypoetry/venv/bin/poetry时,完整路径应为root/.local/share/pypoetry/venv/bin/poetry,而实际上却是app/.local/share/pypoetry/venv/bin/poetry
定义完整路径或将root目录设置为工作目录

相关问题