0
votes

I do not know what is the problem here. The Google Cloud Run gives me this error:

Ready condition status changed to False for Revision ezposappapi-00003-miv with message: 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...

I have followed the example from Google here.

This is my Program.cs:

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

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        #if DEBUG
        return Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
        #endif

        #if !DEBUG
        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);
            });
        #endif
    }
}

The container is running at port 8080 at Google Cloud Run. This is my DockerFile:

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "EzPOSAppAPI.dll"]

There is another error from the logs:

Uncaught signal: 6, pid=1, tid=1, fault_addr=0.
{
  "textPayload": "Uncaught signal: 6, pid=1, tid=1, fault_addr=0.",
  "insertId": "610768a70008741958ceffb7",
  "resource": {
    "type": "cloud_run_revision",
    "labels": {
      "service_name": "ezposappapi",
      "project_id": "digilife-software",
      "configuration_name": "ezposappapi",
      "revision_name": "ezposappapi-00003-miv",
      "location": "asia-southeast2"
    }
  },
  "timestamp": "2021-08-02T03:38:15.553951292Z",
  "severity": "ERROR",
  "labels": {
    "instanceId": "00bf4bf02dc90720030c58d69fb03c88335dbcae23dbf8624600d5b3cf9e014ff1bbb38d2b764f1cccc3ada43f158ca49843c576aca917efaa41d8bf51daf846"
  },
  "logName": "projects/digilife-software/logs/run.googleapis.com%2Fvarlog%2Fsystem",
  "receiveTimestamp": "2021-08-02T03:38:15.555133567Z"
}

There is no more error message. I do not know how to fix this. Please help.


EDIT

This is my current attempt it works on my local machine:

Program.cs:

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

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
        }
    }
}

Dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["EzPOSAppAPI.csproj", "EzPOSAppAPI/"]
RUN dotnet restore "EzPOSAppAPI/EzPOSAppAPI.csproj"

WORKDIR "/src/EzPOSAppAPI"
COPY . .

RUN dotnet build "EzPOSAppAPI.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "EzPOSAppAPI.dll"]

This is the run command:

docker run -dt -v "C:\Users\astef\vsdbg\vs2017u5:/remote_debugger:rw" -v "C:\Users\astef\Documents\Projects\C Sharp\EzPOSAppAPI\EzPOSAppAPI\EzPOSAppAPI:/app" -v "C:\Users\astef\Documents\Projects\C Sharp\EzPOSAppAPI\EzPOSAppAPI:/src/" -v "C:\Users\astef\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" -v "C:\Users\astef\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro" -v "C:\Users\astef\.nuget\packages\:/root/.nuget/fallbackpackages3" -v "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages:/root/.nuget/fallbackpackages" -v "C:\Program Files (x86)\Microsoft\Xamarin\NuGet\:/root/.nuget/fallbackpackages2" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "ASPNETCORE_LOGGING__CONSOLE__DISABLECOLORS=true" -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=https://+:443;http://+:80" -e "NUGET_PACKAGES=/root/.nuget/fallbackpackages3" -e "NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages;/root/.nuget/fallbackpackages2;/root/.nuget/fallbackpackages3" -P --name EzPOSAppAPI --entrypoint tail ezposappapi:dev -f /dev/null

How can I set this up on Google Cloud Run?

1
Try using UseUrls("http://*:8080"), I see you are exposing 80 and using 8080 in your code. Both are not same port, Are you aware of that?Anirudha Gupta
Hello I thought Environment.GetEnvironmentVariable("PORT") will automatically assign 8080. Anyway I did like what you asked me to change into UseUrls("http://*:8080"), but I got the same error. I am not sure if the change is actually being uploaded. I did this docker tag ezposappapi asia.gcr.io/digilife-software/ezposappapi to update the build and docker push asia.gcr.io/digilife-software/ezposappapi to push the new build. I am not sure if this is correct. I am new to docker.Alvin Stefanus
I wish you one thing, Just grab any working code from Github and try that, If that is working compare it with your code. This way your can easily solve your problem.Anirudha Gupta
1) Signal 6 means your program called abort() which means it crashed or had a runtime error. 2) You need to build the container. Then you need to redeploy to Cloud Run. Reread the tutorial that you used to see how to deploy. Use gcloud builds submit followed by gcloud run deploy. 3) Delete the #if sections. Post clean code so that we know which section is used. 4) Include Startup.cs 5) Remove the EXPOSE statements from your Dockerfile.John Hanley
Port 443 is normally used for HTTPS. Enabling that port will accomplish nothing as the only scheme that Cloud Run supports to your application is HTTP. Also, Cloud Run only supports one application port which defaults to 8080 but is configurable.John Hanley

1 Answers

1
votes

Ok here is my problem. I got signal 6 which means there is something wrong with my code. Apparently when the application starts, I tried to access the database. There was something wrong with the database access (I did not allow the connection from the database instance).

I have made some changes in the Dockerfile and also the Program.cs.

Here is my working Dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["EzPOSAppAPI.csproj", "EzPOSAppAPI/"]
RUN dotnet restore "EzPOSAppAPI/EzPOSAppAPI.csproj"

WORKDIR "/src/EzPOSAppAPI"
COPY . .

RUN dotnet build "EzPOSAppAPI.csproj" -c Release -o /app/build

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

FROM base AS final
ENV ASPNETCORE_URLS=http://*:${PORT}
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "EzPOSAppAPI.dll"]

I added ENV ASPNETCORE_URLS=http://*:${PORT} before ENTRYPOINT.

Also I swapped the:

WORKDIR "/src/EzPOSAppAPI"
COPY . .

The Docker template from Visual Studio 2019 is messed up, it only works for debugging.

Also I changed:

COPY ["EzPOSAppAPI/EzPOSAppAPI.csproj", "EzPOSAppAPI/"] 

to

COPY ["EzPOSAppAPI.csproj", "EzPOSAppAPI/"]

Then this is my Program.cs:

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        #if DEBUG
        return Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
        #endif

        #if !DEBUG
        return Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>()
                    .ConfigureKestrel(options =>
                    {
                        var port = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "8080");
                        options.Listen(IPAddress.Any, port);
                    });
            });
        #endif
    }

I have to put #if DEBUG and #if !DEBUG because when debugging, it will not work if I am using the code for production.

I hope this helps someone who uses the default Docker template from Visual Studio 2019.