简体   繁体   English

如何使用 Google Cloud Run 运行“hello world”python 脚本

[英]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.我正在尝试学习如何使用 Google Cloud 安排 python 脚本。 After a bit of research, I've seen many people suggest Docker + Google Cloud Run + Cloud Scheduler .经过一些研究,我看到很多人建议使用 Docker + Google Cloud Run + Cloud Scheduler I've attempted to get a "hello world" example working, to no avail.我试图让“hello world”示例正常工作,但无济于事。

Code代码

hello.py你好.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 使用 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 将镜像推送到 Artifact Registry

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

    在此处输入图像描述

  6. 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.用户提供的容器无法启动并侦听由 PORT=8080 环境变量提供的端口。

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.任何帮助将不胜感激。

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 .我一直在尝试将我的脚本部署为 Cloud Run Service I should've tried deploying it as a Cloud Run Job .我应该尝试将其部署为 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.令人困惑的是,您无法直接从 Artifact Registry 部署云运行作业。 You have to start from the cloud run dashboard.您必须从云运行仪表板开始。

Your Flask application should be something like below:您的 Flask 应用程序应如下所示:

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有关分步说明,请参阅此官方文档: 将 Python 服务部署到 Cloud Run

There is a plugin called: Cloud Code IDE plugin which makes the test and deployment easy.有一个插件叫做: Cloud Code IDE plugin可以让测试和部署变得简单。 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.我将它用于VS 代码,一旦完成初始设置和权限,只需单击几下,您就可以在本地运行,从本地实例调试和部署云运行服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM