Docker container with CMD pipenv run ...
fails on Google Cloud Run but runs locally.
As described below, the Dockerfile contains a step RUN pipenv install
. But logs from the cloud run shows that pipenv run
triggers the installation of a new virtualenv.
This is not the case when running the container on my local machine.
Perhaps the issue is to do with the gVisor sandbox used on managed gcloud run.
The overview of system compatibilty could hold the key but I have no idea.
I have modified the troubleshoot example for Google Cloud Run to use pipenv instead of pip.
So the modification I made is to replace requirement.txt
with Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
pytest = "==4.6.6"
gunicorn = "==20.0.4"
Flask = "==1.1.1"
[requires]
python_version = "3.8"
and Dockerfile
```FROM python:3.8
RUN pip install pipenv
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY Pipfile Pipfile.lock ./
RUN pipenv install --deploy --ignore-pipfile
COPY . .
CMD pipenv run gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
building the image and pushing to GCR with *PROJECT_ID*, then running locally
PORT=8080 && docker run --rm -e $PORT -p 9000:$PORT \
-e NAME="Robust World" \
gcr.io/PROJECT_ID/hello-service
```
works fine, but creating the cloud run service fails.
The pipenv run
command apparantly doesn't find the virtualenv, starts creating a new one which doesn't contain requirement gunicorn
and breaks.
Seeing this too.
Solved by adding to Dockerfile:
ENV PIPENV_VENV_IN_PROJECT 1
from
https://pipenv.pypa.io/en/latest/install/#virtualenv-mapping-caveat
This is probably a Cloud Run bug.
I reported the issue internally. If it is due to a gVisor limitation, it'd be best to open an issue at https://github.com/google/gvisor
I considered raising with gVIsor but I figured I'd first ask for support here.
Afterall, it might be expected behaviour for gVisor given the documented compatibility.
Perhaps someone familiar with pipenv internals is able to spot the reason.
Hi, (Cloud Run PM here)
After further investigation, it does not seem to be an issue with gVisor as the container runs fine with $ docker run --runtime=runsc
We are investigating why it fails on Cloud Run
(Internal issue 154368323 if there is any googler in the thread).
I also have issues deploying to Cloud Run.
This is my code:
FROM python:3.8.2
RUN pip install pipenv
# Create app directory
ENV APP_HOME /app
WORKDIR $APP_HOME
ENV PYTHONPATH=$PYTHONPATH:$APP_HOME
# Copying this separately prevents re-running pip install on every code change.
COPY Pipfile .
COPY Pipfile.lock .
#ENV PIPENV_VENV_IN_PROJECT=1
RUN pipenv install --deploy --ignore-pipfile
ENV PATH=/root/.local/share/virtualenvs/app-4PlAip0Q/bin:$PATH
# Copy utility files
COPY .env.local .
COPY .env.database.local .
COPY alembic.ini .
# Copy over bash file
COPY startup.sh .
RUN chmod +x ./startup.sh
# Copy the rest of the files over
COPY . .
EXPOSE $PORT
ENTRYPOINT ["bash", "/app/startup.sh"]
startup.sh:
alembic upgrade head
python /app/bin/web.py
compose:
version: '3.8'
services:
api-db:
env_file:
- .env.database.local
image: postgres:alpine
ports:
- "5432:5432"
restart: always
api-pgadmin:
container_name: pgadmin
image: dpage/pgadmin4:latest
environment:
- PGADMIN_DEFAULT_EMAIL=x
- PGADMIN_DEFAULT_PASSWORD=x
ports:
- "5050:80"
depends_on:
- api-db
api-redis:
image: "redis:alpine"
api:
build:
context: '.'
dockerfile: Dockerfile
volumes:
- .:/app
env_file:
- .env.local
ports:
- "8080:8080"
depends_on:
- api-db
- api-redis
restart: always
error:
Traceback (most recent call last):
File "/app/bin/web.py", line 1, in <module>
import uvicorn
ModuleNotFoundError: No module named 'uvicorn'
I tried using pipenv run
and as stated, it doesn't seem to find the virtual machine. I tried using ENV PIPENV_VENV_IN_PROJECT 1
but then my local container cannot find the alembic command. Does anyone have a solution?
@sjosegarcia Uncomment and fix the line #ENV PIPENV_VENV_IN_PROJECT=1
on your Dockerfile to ENV PIPENV_VENV_IN_PROJECT 1
and try to use pipenv run alembic upgrade head
and pipenv run python /app/bin/web.py
on your startup.sh file.
Most helpful comment
Solved by adding to Dockerfile:
ENV PIPENV_VENV_IN_PROJECT 1
from
https://pipenv.pypa.io/en/latest/install/#virtualenv-mapping-caveat
This is probably a Cloud Run bug.