'Visual Studio 2019 - Docker 4.1.1 - Web App Stops Working After Adding Container Orchestration

This is sort of strange. As soon as I add docker orchestration in Visual Studio 2019 my Web Application stops working. These are the exact steps that I am taking:

  1. Launch Visual Studio 2019 (16.11.3)
  2. Create new Project
  3. Select "ASP.NET Core Web App", and click on Next
  4. Accept default Project Name and Location, and click on Next
  5. Specify, target framework as .NET 5.0 (Current), Auth Type: Individual Accounts, Check the box for Configure for HTTPS. At this moment I am not checking the Enable docker and enable razor runtime. Click on Create
  6. On the welcome screen click on Connected services, and configure on the Secrets.json (local) (finish, close that)
  7. Right-Click on my project, add, Container Orchestration Support
  8. Select "Docker Compose" as the orchestrator, and linux as the target os
  9. It appears that Visual Studio creates a docker-compose project to the solution, as well as some dockerfiles to the project. The output also shows that the containers are being built and eventually says they are ready.

However my docker desktop shows them in this state

enter image description here

The error shows as follows

enter image description here

The text of that error shows:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]

      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]

      No XML encryptor configured. Key {a6907c05-45f2-4dc3-93ac-5d59ce2b2873} may be persisted to storage in unencrypted form.

crit: Microsoft.AspNetCore.Server.Kestrel[0]

      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'.

      For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

         at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions)

         at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions)

         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)

         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context)

         at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)

         at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)

Unhandled exception. 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'.

For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions)

   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions)

   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)

   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context)

   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)

   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)

   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)

   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)

   at WebApplication7.Program.Main(String[] args) in /src/WebApplication7/Program.cs:line 16

I was expecting since I didn't do anything custom yet that the orchestrator would just simply "work" at this point but it doesn't.

I noticed that my Trust ASP.NET Core SSL certificate is "Prompt Me", but I never receive the prompt

enter image description here

I also try and do what is suggested in the error. So I press the trash can icon in my docker desktop for the orchestration. I then modify the Dockerfile inside of the project as follows, notice the extra run that I add before the entry point there.

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["WebApplication7/WebApplication7.csproj", "WebApplication7/"]
RUN dotnet restore "WebApplication7/WebApplication7.csproj"
COPY . .
WORKDIR "/src/WebApplication7"
RUN dotnet build "WebApplication7.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN dotnet dev-certs https
ENTRYPOINT ["dotnet", "WebApplication7.dll"]

I save the file and rebuild the solution and press the debug / start button (F5) for docker compose. It says the containers are ready again but now there are no ports exposed, thus no where to browse to, and no logs. I can access the cli for the container but that's it.

enter image description here

What am I doing wrong? Why is this not just simply working OOB with Visual Studio?

Something that does work though is if I take that extra RUN line out of my Dockerfile that I added, then set my web project as the startup project, I am able to debug and run that. But again, once I set the docker-compose project as the startup project and try to debug that, I am dead in the water again.



Solution 1:[1]

Inside of your project go to Solution Explorer > Solution > Properties > launchsettings.json.

There should be a Docker section that Visual Studio adds automatically, if not add the Docker launch settings below. You can choose which ports you want your app to listen on.

    "Docker": {
      "commandName": "Docker",
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "publishAllPorts": true,
      "useSSL": true,
      "DockerfileRunArguments": "add custom arguments here such as --network my-network",
      "httpPort": 5000,
      "sslPort": 5001
    }
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s