在Google Cloud Run上运行Go程序而不监听传入的HTTP请求。

huangapple go评论75阅读模式
英文:

Running go program on Google Cloud Run without listening for incoming HTTP requests

问题

我写了一个 Go 程序,默认情况下不需要进行外部的 HTTP 调用。我尝试将其部署到 Google Cloud Run 上,但收到了以下错误信息:

用户提供的容器无法启动并监听由 PORT=8080 环境变量定义的端口。此版本的日志可能包含更多信息。

我理解这是因为我的代码没有提供一个端口。根据这个答案所述:

容器必须在由 Cloud Run 定义并在 $PORT 环境变量中提供的端口上监听传入的 HTTP 请求。

我的问题是,如果我不想定义任何端口,只想运行与本地相同的代码,我该怎么办?是否有其他解决方案可以在不定义端口的情况下部署我的代码,或者我必须无论如何添加端口才能在 Cloud Run 上运行代码?

英文:

I wrote a Go program which doesn't need to retrieve external http calls at all by default. I tried to deploy it on Google Cloud Run and received the following error:

> The user-provided container failed to start and listen on the port
> defined provided by the PORT=8080 environment variable. Logs for this
> revision might contain more information.

I understand it happens because my code doesn't provide a port. As this answer states:

> container must listen for incoming HTTP requests on the port
> that is defined by Cloud Run and provided in the $PORT environment
> variable

My question is what can I do if wouldn't like define any ports and just want to run the same code I run locally? Is there an alternate solution to deploy my code without it, or I must add it anyway if I want run the code from Cloud Run?

答案1

得分: 2

对于不需要HTTP监听器(HTTP服务器)的容器,请使用Cloud Run Jobs。

  • Cloud Run Jobs处于预览状态。
  • 您的Go程序必须以退出代码0表示成功,非零表示失败。
  • 您的容器不应监听端口或启动Web服务器。
  • 环境变量与Cloud Run不同。
  • 容器实例在容器实例退出、任务超时到达或容器崩溃之前一直运行。任务超时默认为10分钟,最长为一小时。

Cloud Run - 创建作业

英文:

For containers that do not require an HTTP listener (HTTP server), use Cloud Run Jobs.

  • Cloud Run Jobs is in preview.
  • Your Go program must exit with exit code 0 for success and non-zero for failure.
  • Your container should not listen on a port or start a web server.
  • Environment variables are different from Cloud Run.
  • Container instances run until the container instance exits, until the task timeout is reached, or until the container crashes. Task timeout default is 10 minutes, max is one hour.

Cloud Run - Create jobs

huangapple
  • 本文由 发表于 2022年8月27日 05:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73506733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定