Home > Software design >  I cannot resolve "Failed to start and then listen on the port defined by the PORT environment v
I cannot resolve "Failed to start and then listen on the port defined by the PORT environment v

Time:12-09

I know there are 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.UI\Dockerfile --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

CodePudding user response:

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!

CodePudding user response:

The port that your dotnet container is listening on (default) is set in properties\launchsettings.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 properties\launchsettings.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);
                        });
                }
        }
}
  • Related