Here is my 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"]
I can build and deploy this locally no problem and the server starts. However, when I deploy to Cloud Run, I get the following error and the container fails:
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'
Does anybody have any idea why this successfully works locally but is missing a dependency in Cloud Run? One weird thing is that I explicitly telling docker to NOT use a virtual environment in the Dockerfile. This works when I run the image locally, but on Google Cloud it insists on building a virtual environment anyways. Is there some sort of incompatibility with Google Cloud Run's version of Docker and poetry that I'm missing here?