Search code examples
asp.netdockerazure-web-app-service

what URL should Kestrel listen to in a docker container on Azure App Service


I have an ASP.NET Core in a docker container that will be hosted on Azure App Service. What URL should I have Kestrel, which is inside the container, listen on when deploying to Azure App Service?

My mental model is I could use localhost:8080, then bind that container port 8080 to port 443 of Azure App Service via -p 443:8080. So when visiting https://myapp.azurewebsites.net, that 443 will be forwarded to 8080 that Kestrel is listening to.

ChatGPT suggested to listen to wildcard 0.0.0.0, but my gut feeling of both localhost and 0.0.0.0 are wrong

So what is the correct way to do? Thank you. I am self-taught and I only have very basic networking knowledge so please explain like I'm 5, and give keywords so that I could research more. Thank you


Solution

  • You definitely want to bind to (that's what it's called) 0.0.0.0. It specifies where you'll accept connections from. In a container, localhost is the container itself, so if you bind to localhost, your application will only accept connections coming from inside the container. That's not what you want.

    As for what port to use inside the container, it doesn't really matter as you'll map it to a host port when you run the container. The aspnet images from Microsoft are configured to accept HTTP traffic on port 8080 for .NET 8 and on port 80 for .NET 7 and earlier.

    Since you map to host port 443 it looks like you want to use HTTPS and not HTTP. To do that, you need to configure ASP to do that, as the default is HTTP only. You can configure ASP by setting the environment variable ASPNETCORE_URLS to something like https://+:8443. Then it'll listen for HTTPS on port 8443. If you do this, I recommend listening on another port than 8080 since that's conventionally used for HTTP traffic.

    To use HTTPS you also need to configure certificates. You can read more about it here.

    Short answer: To get a .NET 8 aspnet container up and running, this command should do the trick:

    docker run -d -p 8080:8080 -e ASPNETCORE_ENVIRONMENT=Development my-image
    

    The environment needs to be set to Development if you want Swagger to be available. Swagger is only available in the Development environment and the default is that a docker container is not development.