1

When Cloud Run runs a container image, the container fails differently than when I run it locally.

I added this try/except in app/main.py to debug the divergent behaviors:

print(f'cwd is {os.getcwd()}')
try:
    from .make_sticker.config import StickerConfig
    print('relative worked')
except:
    from make_sticker.config import StickerConfig
    print('except worked')

When I run the container locally, the app logs 'except worked'. When I run the container in Cloud Run, the app fails out of the except and does not log anything.

This is my Dockerfile:

FROM python:3.12

WORKDIR /code

COPY app/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY app/ /code/app

ENV PYTHONPATH=/code/app

WORKDIR /code/app

RUN ls -la

EXPOSE 5001
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5001"]

What is going on here?

8
  • 1
    Do you use Cloud Run execution runtime 1st or 2nd generation? If it's 1st, can you try on the 2nd generation and let us know? Commented Nov 11, 2024 at 9:41
  • Thanks Guillaume! I tried it on the 2nd generation and got the same result. Commented Nov 11, 2024 at 15:27
  • I re-read your title, and you mention a "module not found error". Are you sure that your from.... is not what is crashing your app? In addition, are you running your code or the container locally? What's your container parameters? Commented Nov 11, 2024 at 15:50
  • 1
    Your question is missing your crashing stacktrace, and its not clear if you run the exact same container locally as in cloud run (ie you didnt build it multiple times) Commented Nov 12, 2024 at 8:52
  • 2
    It is possible for one same container image to exhibit different behavior when run in multiple places because, although the container image content is the same, the runtime configuration may and the kernel will differ. Per @guillaume-blaquiere comment, Cloud Run v1 uses gVisor to implement Linux syscalls and so is more likely (than v2) to exhibit different behavior. A repro would be helpful plus more details (deployment, logging and stacktrace) to debug. Commented Nov 12, 2024 at 16:48

1 Answer 1

0

I am going to debug further to understand what's going on, but by simply changing the name of the parent python module from app to fastapp, the import worked in the Cloud Run environment.

Update 1:
There doesn't seem to be anything implicitly wrong with using the name app. I deployed a container build from this minimal reproducible test repo and using app as the parent name worked just fine.

hmm, I wonder if one of the imports in my real app clobbers the app module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.