1
votes

I'm having issues trying to debug a docker container pointed at the "Production" ASPNETCORE_ENVIRONMENT in Visual Studio. The "Development" environment works fine. I'm trying to debug against a production container because we are having issues with the different appsettings file per environment.

This is my error:

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'. For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

I've looked through a few articles but nothing seems to work when debugging against production. When I remove https from the launchSettings.json the site doesn't run at all.

https://github.com/dotnet/dotnet-docker/blob/master/samples/host-aspnetcore-https.md

Unable to configure ASP.NET HTTPS endpoint in Windows docker container

https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.1&tabs=visual-studio#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos

Environment:

Windows 10
Linux Containers
ASP.NET Core 3.1

launchSettings:

"Docker": {
  "commandName": "Docker",
  "launchBrowser": true,
  "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Production",
    "ASPNETCORE_URLS": "https://+:443;http://+:80",
  },
  "httpPort": 51934,
  "useSSL": true,
  "sslPort": 44349
}

DockerFile

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.2-bionic AS base
WORKDIR /app
EXPOSE 80
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y tzdata
RUN apt install -y --allow-unauthenticated \
    libc6-dev \
    libgdiplus \
    libx11-dev \
 && rm -rf /var/lib/apt/lists/*

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /src
COPY src .
RUN dotnet restore "ExampleApp.Web/ExampleApp.Web.csproj"
COPY . .
WORKDIR "/src/ExampleApp.Web"
RUN dotnet dev-certs https --trust
RUN dotnet build "ExampleApp.Web.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENV ASPNETCORE_ENVIRONMENT Production

ENTRYPOINT ["dotnet", "ExampleApp.Web.dll", "--environment=Production"]

enter image description here

1

1 Answers

2
votes

Some options

Option 1: inside the app, configure like so, it should work

WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls(YourWebAppUrls)
                .UseKestrel()
                .ConfigureKestrel(options =>
                {
                    options.ListenAnyIP(51934);  // whatever your port
                })
                .UseIIS()

Option 2:

In your build tasks you can add this to the command line see here

Close your browsers so that they do not cache the certificate because that will cause other issues.

On the commandline run this

dotnet dev-certs https --clean

then run

dotnet dev-certs https -t

Option 3: Self signed Cert


Option 4: Run these commands from here

dotnet dev-certs https -ep ${HOME}/.aspnet/https/aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust

Windows using Windows containers Generate certificate and configure local machine:

dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust

Run the Container Image with Core configured for HTTPS:

docker pull mcr.microsoft.com/dotnet/core/samples:aspnetapp
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="password" -e ASPNETCORE_Kestrel__Certificates__Default__Path=\https\aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:C:\https\ mcr.microsoft.com/dotnet/core/samples:aspnetapp