skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. Your Flask application should be something like below:

    import os
    
    from flask import Flask
    
    app = Flask(__name__)
    
    
    @app.route("/")
    def hello_world():     
        return "Hello World!"
    
    
    if __name__ == "__main__":
        app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
    

    See this official documentation for step by step instruction: Deploy a Python service to Cloud Run

    There is a plugin called: Cloud Code IDE plugin which makes the test and deployment easy. I am using it for VS code, once the initial setups and permissions are taken care, few clicks, you will be able to run locally, debug and deploy Cloud run services from your local instance.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search