Kestrel HTTPS Error - .Net Core and Docker
The following is another commonly-posted question in Stack Overflow, which I’ve been looking at while debugging the same problem:
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
The most obvious cause would be Kestrel trying to use invalid or expired HTTPS certificates. However, looking more closely, my error message was preceded by:
[...]
[15:25:25 FTL] Unable to start Kestrel.
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date. To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
[...]
And is followed by:
[...] Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
So, the real problem could have been anything from Kestrel simply being unable to start, which is less likely, to Kestrel being unable to bind the service to a port. The easiest thing to check is that the development certificates are valid anyway, using the following:
dotnet dev-certs https --clean
dotnet dev-certs https
dotnet dev-certs https --trust
And, after this, restarting Visual Studio.
The Actual Problem
As it turned out, the problem was caused by the exposed ports and port mappings being incorrectly defined in the Docker configuration. First, shift the ASPNETCORE_URLS variables from the Dockerfile to docker-compose.yml environment section. This is good practice, in my opinion, because all environment variables should ideally be in one place. Next, explicitly set the Kestrel endpoint.
environment:
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=5001
- Kestrel__Endpoints__Http__Url=http://*:80
- ASPNETCORE_ENVIRONMENT= Development