skip to Main Content

I know there are many posts about this, but I still can’t resolve this. It’s a .Net Core 3.1 app using React.

npm run build completes successfully.

I have PORT=8080 in my .env file

I have configured my service to run on port 8080.
enter image description here

I have my Dockerfile set to expose 8080:

enter image description here

but I still get the following:
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’ve checked the logs, but it just shows the same message with a link to itself:
enter image description here

So the logs seem useless.

I am deploying as follows:

  1. docker build -f foobarDataViz.UIDockerfile --force-rm -t foobardataviz.ui . --no-cache
    This builds successfully.
  2. docker run -d -p 8080:80 --name foobar foobardataviz.ui This runs successfully and I can browse locally on port 8080
  3. docker push us-central1-docker.pkg.dev/foobar-poc/foobar/foobardataviz.ui:latest
  4. I then go to Cloud Run and click "Edit and Deploy New Revision", which does some processing for a little while, but fails with the above error

So, the error suggests it’s not exposing the port, or it’s just failing to start altogether. Could it be that my docker image is not compatible with how I’ve set up Cloud Run?

Thanks in advance

2

Answers


  1. your #2: your port is served on the 80 in your container, and exposed as 8080 on your local machine. You have your issue here.

    Use the port 80 when you configure your Cloud Run service. This port must be listen by Cloud Run, not the 8080 that is not listen by your container. However, I’m not sure it will work.


    If you look for a .NET solution about the configuration, I absolutely don’t know .NET framework.


    EDIT 1

    If you perform that command

    docker run -d -p 8080:80 ...
    

    That means you forward the internal port 80 outside Docker and expose it on the 8080. That’s why you can test your app locally on the 8080 port.

    With Cloud Run, you have a similar configuration: you can set the port to listen inside your container. (Take your 1st screenshot, it’s the 4th line). You can change that when you deploy your Cloud Run service

    enter image description here

    Try to put the 80 port (I say try because sometimes the port 80 requires additional privilege).

    Now, why your .NET app exposes the port 80 and not the port 8080, as I said, I absolutely don’t know!

    Login or Signup to reply.
  2. The port that your dotnet container is listening on (default) is set in propertieslaunchsettings.json. The default configuration look like this:

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

    Note: The Cloud Run instance (container) should not listen on HTTPS. The Cloud Run GFE forwards traffic using HTTP.

    Modify propertieslaunchsettings.json as follows to use the default port which is 8080:

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

    A better solution is to modify CreateHostBuilder() to read the port from the environment.

    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);
                            });
                    }
            }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search