[英]Cloud Run: deploying spring docker image causing error; Failed to start and then listen on the port defined by the PORT environment variable
[英]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
我將 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.
所以日志似乎沒用。
我正在部署如下:
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
這構建成功。docker run -d -p 8080:80 --name foobar foobardataviz.ui
運行成功,可以在8080端口本地瀏覽docker push us-central1-docker.pkg.dev/foobar-poc/foobar/foobardataviz.ui:latest
因此,該錯誤表明它沒有暴露端口,或者它只是無法完全啟動。 會不會是我的 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.