Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a "hello world" python script with Google Cloud Run

Forgive my ignorance..

I'm trying to learn how to schedule python scripts with Google Cloud. After a bit of research, I've seen many people suggest Docker + Google Cloud Run + Cloud Scheduler. I've attempted to get a "hello world" example working, to no avail.

Code

hello.py

print("hello world")

Dockerfile

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "hello.py"]

Steps

  1. Create a repo with Google Cloud Artifact Registry

    gcloud artifacts repositories create test-repo --repository-format=docker \
    --location=us-central1 --description="My test repo"
    
  2. Build the image

    docker image build --pull --file Dockerfile --tag 'testdocker:latest' .
    
  3. Configure auth

    gcloud auth configure-docker us-central1-docker.pkg.dev
    
  4. Tag the image with a registry name

    docker tag testdocker:latest \
    us-central1-docker.pkg.dev/gormanalysis/test-repo/testdocker:latest
    
  5. Push the image to Artifact Registry

    docker push us-central1-docker.pkg.dev/gormanalysis/test-repo/testdocker:latest
    

    enter image description here

  6. Deploy to Google Cloud Run

    enter image description here enter image description here enter image description here

Error

At this point, I get the error

The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable.

I've seen posts like this which say to add

app.run(port=int(os.environ.get("PORT", 8080)),host='0.0.0.0',debug=True)

but this looks like a flask thing, and my script doesn't use flask. I feel like i have a fundamental misunderstanding of how this is supposed to work. Any help would be appreciated it.

like image 863
Ben Avatar asked Oct 19 '25 12:10

Ben


1 Answers

UPDATE
I've documented my problem and solution in much more detail here »


I had been trying to deploy my script as a Cloud Run Service. I should've tried deploying it as a Cloud Run Job. The difference is that cloud run services require your script to listen for a port. jobs do not.

enter image description here

Confusingly, you cannot deploy a cloud run job directly from Artifact Registry. You have to start from the cloud run dashboard.

like image 72
Ben Avatar answered Oct 21 '25 01:10

Ben