【发布时间】:2021-10-11 11:39:24
【问题描述】:
这是我的 Dockerfile:
# Use lightweight Python image
FROM python:3.9-slim
ARG DOCKER_ENV
# PYTHONFAULTHANDLER=1 - Display trace if a sefault occurs.
# PYTHONUNBUFFERED=1 - Allow statements and log messages to immediately appear in the Knative logs
# PIP_NO_CACHE_DIR=off - Disable pip cache for smaller Docker images.
# PIP_DISABLE_PIP_VERSION_CHECK=on - Ignore pip new version warning.
# PIP_DEFAULT_TIMEOUT=100 - Give pip longer than the 15 second timeout.
ENV DOCKER_ENV=${DOCKER_ENV} \
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100
# Install poetry
RUN pip install
# Set working directory in container to /app
WORKDIR /app
# Copy only dependency requirements to container to cache them in docker layer
COPY poetry.lock pyproject.toml /app/
# Don't need virtualenv because environment is already isolated in a container
RUN poetry config virtualenvs.create false
# Install production dependencies
RUN poetry install --no-dev --no-ansi
# Copy app into container
COPY . /app
# Run server
CMD [ "poetry", "run" , "python", "api.py"]
我可以在本地构建和部署它没有问题,服务器启动。但是,当我部署到 Cloud Run 时,我收到以下错误并且容器失败:
Creating virtualenv indie-9TtSrW0h-py3.9 in /home/.cache/pypoetry/virtualenvs
File "/app/api.py", line 6, in <module>
import jwt
ModuleNotFoundError: No module named 'jwt'
有人知道为什么这在本地成功运行但在 Cloud Run 中缺少依赖项吗?一件奇怪的事情是,我明确告诉 docker 不要在 Dockerfile 中使用虚拟环境。这在我在本地运行映像时有效,但在 Google Cloud 上它仍然坚持构建虚拟环境。与 Google Cloud Run 的 Docker 版本和我在这里缺少的诗歌是否存在某种不兼容?
【问题讨论】:
-
在文件requirements.txt中指定依赖关系。
-
你试过你的容器是另一个环境吗?例如 Compute Engine,或者 Cloud Shell?
-
@crazysnake99 你解决了吗?我也遇到了这个问题。
-
@crazysnake99 我尝试让步并将
poetry config virtualenvs.create从false设置为true,但没有帮助。 -
@JohnHanley 他为什么要在
requirements.txt中指定要求? Poetry 以与pip相同的方式将要求安装到容器中。似乎出于某种原因,在 Cloud Run 上,来自Dockerfile的CMD命令是孤立的,与其余命令处于不同的上下文中。
标签: python docker google-cloud-run