簡體   English   中英

我無法解決“無法啟動並偵聽 PORT 環境變量定義的端口”。 帶有 Docker 圖像的 Google Cloud Run 錯誤

[英]I cannot resolve "Failed to start and then listen on the port defined by the PORT environment variable." error in Google Cloud Run with a Docker image

我知道有很多關於這個帖子,但我仍然無法解決這個問題。 這是一個使用 React 的 .Net Core 3.1 應用程序。

npm run build成功完成。

我的 .env 文件中有PORT=8080

我已將我的服務配置為在端口 8080 上運行。 在此處輸入圖像描述

我將 Dockerfile 設置為公開 8080:

在此處輸入圖像描述

但我仍然收到以下信息: Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information. Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

我檢查了日志,但它只顯示相同的消息,並帶有指向自身的鏈接: 在此處輸入圖像描述

所以日志似乎沒用。

我正在部署如下:

  1. docker build -f foobarDataViz.UI\Dockerfile --force-rm -t foobardataviz.ui. --no-cache docker build -f foobarDataViz.UI\Dockerfile --force-rm -t foobardataviz.ui. --no-cache這構建成功。
  2. docker run -d -p 8080:80 --name foobar foobardataviz.ui運行成功,可以在8080端口本地瀏覽
  3. docker push us-central1-docker.pkg.dev/foobar-poc/foobar/foobardataviz.ui:latest
  4. 然后我將 go 到 Cloud Run 並單擊“編輯並部署新修訂版”,它會進行一些處理,但由於上述錯誤而失敗

因此,該錯誤表明它沒有暴露端口,或者它只是無法完全啟動。 會不會是我的 docker 映像與我設置 Cloud Run 的方式不兼容?

提前致謝

您的#2:您的端口在容器中的 80 上提供服務,並在本地計算機上公開為 8080。 你有你的問題在這里。

配置 Cloud Run 服務時使用端口 80。 此端口必須由 Cloud Run 監聽,而不是您的容器未監聽的 8080。 但是,我不確定它會起作用。


如果您尋找有關配置的.NET 解決方案,我絕對不知道.NET 框架。

您的 dotnet 容器正在偵聽的端口(默認)在properties\launchsettings.json中設置。 默認配置如下所示:

"applicationUrl": "https://localhost:5001;http://localhost:5000",

注意:Cloud Run 實例(容器)不應偵聽 HTTPS。 Cloud Run GFE 使用 HTTP 轉發流量。

如下修改properties\launchsettings.json以使用默認端口 8080:

"applicationUrl": "http://0.0.0.0:8080",

更好的解決方案是修改CreateHostBuilder()以從環境中讀取端口。

namespace HelloWorld
{
        public class Program
        {
                public static void Main(string[] args)
                {
                        CreateHostBuilder(args).Build().Run();
                }

                public static IHostBuilder CreateHostBuilder(string[] args)
                {
                        string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
                        string url = String.Concat("http://0.0.0.0:", port);

                        return Host.CreateDefaultBuilder(args)
                                .ConfigureWebHostDefaults(webBuilder =>
                        {
                                webBuilder.UseStartup<Startup>().UseUrls(url);
                        });
                }
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM