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
-
Create a repo with Google Cloud Artifact Registry
gcloud artifacts repositories create test-repo --repository-format=docker --location=us-central1 --description="My test repo"
-
Build the image
docker image build --pull --file Dockerfile --tag 'testdocker:latest' .
-
gcloud auth configure-docker us-central1-docker.pkg.dev
-
Tag the image with a registry name
docker tag testdocker:latest us-central1-docker.pkg.dev/gormanalysis/test-repo/testdocker:latest
-
Push the image to Artifact Registry
docker push us-central1-docker.pkg.dev/gormanalysis/test-repo/testdocker:latest
-
Deploy to Google Cloud Run
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
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.
Confusingly, you cannot deploy a cloud run job directly from Artifact Registry. You have to start from the cloud run dashboard.
Your Flask application should be something like below:
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.