Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker容器上载到gcp云运行,核心web api应用程序不工作_Docker_Google Cloud Platform_Asp.net Core Webapi_Google Cloud Run - Fatal编程技术网

Docker容器上载到gcp云运行,核心web api应用程序不工作

Docker容器上载到gcp云运行,核心web api应用程序不工作,docker,google-cloud-platform,asp.net-core-webapi,google-cloud-run,Docker,Google Cloud Platform,Asp.net Core Webapi,Google Cloud Run,正在尝试使用dotnet core webapi项目上载docker映像 云运行的一个要求是它正在侦听端口8080 我相信我正在这样做,但当我在推到容器注册表后创建云运行服务时,GCP返回: “容器启动失败。无法启动并侦听端口环境变量定义的端口。此版本的日志可能包含详细信息。” 本地我有红隼在8080上收听。还有8080上的集装箱清单。但当我按下其中一个按钮时,我得到启动失败的消息。。。?有什么建议或尝试这样做吗 @wlhee Here is the LOG from cloud run: 2

正在尝试使用dotnet core webapi项目上载docker映像

云运行的一个要求是它正在侦听端口8080

我相信我正在这样做,但当我在推到容器注册表后创建云运行服务时,GCP返回:

“容器启动失败。无法启动并侦听端口环境变量定义的端口。此版本的日志可能包含详细信息。”

本地我有红隼在8080上收听。还有8080上的集装箱清单。但当我按下其中一个按钮时,我得到启动失败的消息。。。?有什么建议或尝试这样做吗

@wlhee Here is the LOG from cloud run:

2019-04-13T05:24:53.462592ZHosting environment: Production
2019-04-13T05:24:53.462657ZContent root path: /app
2019-04-13T05:24:53.462678ZNow listening on: http://[::]:80
2019-04-13T05:24:53.462697ZApplication started. Press Ctrl+C to shut down.
2019-04-13T05:28:48.973934834ZContainer terminated by the container manager on signal 9.

"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文件

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["simplecore.csproj", "simplecore/"]
RUN dotnet restore "simplecore/simplecore.csproj"
COPY . .
WORKDIR "/src/simplecore"
RUN dotnet build "simplecore.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "simplecore.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "simplecore.dll"]
~这是我的核心应用程序的主要功能
公共静态void Main(字符串[]args)
{
//CreateWebHostBuilder(args.Build().Run();
var host=new WebHostBuilder()
.UseKestrel()
.UseContentRoot(目录.GetCurrentDirectory())
//.Useii整合()
.UseStartup()
.useURL(“http://0.0.0.0:8080/")
.Build();
host.Run();
}

以下解决方案对我有效:

在Dockerfile中修改行

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "simplecore.dll"]
通过添加
ENV

FROM base AS final
ENV ASPNETCORE_URLS=http://*:${PORT}
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "simplecore.dll"]
添加运行状况控制器以侦听根路由:

[Route("/")]
[ApiController]
public class HealthController : ControllerBase
{

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return Ok();
    }
}

重建和部署

IJB的答案很贴切,对我们来说很有效,但在我们的案例中,我们使用的是ASP.NET Core 3.0,因此我们不得不稍微修改Program.cs,如下所示:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                        .ConfigureKestrel(options =>
                        {
                            var port = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "80");
                            options.Listen(IPAddress.Any, port);
                        });
                });
    }
还有,这让我们困惑了一段时间,如果您要部署到云运行,
gcloud beta-run deploy
不会推送Docker映像,而是重用已部署的映像。
这让我们非常困惑,直到我们意识到它试图部署的Docker映像具有旧映像ID。因此,要部署到容器注册表,然后部署到云运行,您需要执行以下操作:

  • 构建Docker映像:
  • 将上面的“我的web api”替换为步骤1中使用的

  • 部署到云运行:

  • 您也可以按照相同的模式注入其他环境变量。无论如何,希望这能帮助您解决令人困惑的“容器启动失败”错误。

    您看到“日志”中打印的日志了吗?@wlhee这是来自云运行的日志:
    2019-04-13T05:24:53.462592z托管环境:生产2019-04-13T05:24:53.462657z内容根路径:/app 2019-04-13T05:24:53.462678ZNow正在侦听:http://[:]:80 2019-04-13T05:24:53.462697应用程序已开始。按Ctrl+C组合键关闭。2019-04-13T05:28:48.973934834Z容器在信号9上被容器管理器终止。
    很抱歉格式化看起来应用程序正在侦听端口80而不是8080?即使我尝试让应用程序侦听端口8080,它也失败请遵循并确认您的容器在本地运行。
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var port = Environment.GetEnvironmentVariable("PORT");
    
            return WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseKestrel()
                .ConfigureKestrel((context, options) =>
                {
                    options.Listen(IPAddress.IPv6Any, Convert.ToInt32(port));
                });
        }
    
    app.UseMvc(routes => 
    {
        routes.MapRoute("default", "{controller=Health}/{action=Get}");
    });
    
    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>()
                            .ConfigureKestrel(options =>
                            {
                                var port = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "80");
                                options.Listen(IPAddress.Any, port);
                            });
                    });
        }
    
    [Route("/")]
        [ApiController]
        public class HealthController : ControllerBase
        {
            [HttpGet]
            public ActionResult<IEnumerable<string>> Get()
            {
                return Ok();
            }
        }
    
    docker image build -t my-web-api -t gcr.io/<your project ID here>/my-web-api -f Dockerfile .
    
    docker push gcr.io/<your project ID here>/my-web-api:latest
    
    gcloud beta run deploy --image gcr.io/<your project ID here>/my-web-api --region us-central1
    
    FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
    
    # uncomment if you need to deploy a service account JSON file to access Google Cloud resources
    #ARG GOOGLE_APPLICATION_CREDENTIALS_FILE
    ARG ASPNETCORE_ENVIRONMENT
    
    # uncomment if you need to deploy a service account JSON file
    #ENV GOOGLE_APPLICATION_CREDENTIALS="/app/$GOOGLE_APPLICATION_CREDENTIALS_FILE"
    ENV ASPNETCORE_ENVIRONMENT=$ASPNETCORE_ENVIRONMENT
    
    # uncomment if you need to deploy a service account JSON file
    #COPY "keys/$GOOGLE_APPLICATION_CREDENTIALS_FILE" "/app/"
    
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
    WORKDIR /src
    COPY ["<your project name>.csproj", "./"]
    RUN dotnet restore "<your project name>.csproj"
    COPY . .
    WORKDIR "/src/<your project name>"
    RUN dotnet build "<your project name>" -c Release -o /app
    
    FROM build AS publish
    RUN dotnet publish "<your project name>" -c Release -o /app --self-contained --runtime linux-x64
    
    FROM base AS final
    ENV ASPNETCORE_URLS=http://*:${PORT}
    WORKDIR /app
    COPY --from=publish /app .
    ENTRYPOINT ["dotnet", "<your DLL file here>.dll"]
    
    docker image build -t my-web-api -t gcr.io/<project ID here>/my-web-api -f <project dir>/Dockerfile --build-arg GOOGLE_APPLICATION_CREDENTIALS_FILE="my-service-acccount.json" --build-arg ASPNETCORE_ENVIRONMENT="Development" .