Search code examples
dockerhttps.net-5docker-desktop

Need help enabling SSL for Docker project with SignalR and .NET 5


I've been searching around for two days now and I cannot seem to find a straight answer to this;

How would I go about using SSL in my Docker Container running a .NET 5 project on Windows 10? (only for local development)

Visual Studio 2019 set up my project so that it uses a Dockerfile with Docker Desktop to run my project in a container. Problem is, I need it to serve and send data over HTTPS as my web client (separate project) used HTTPS as well. But after having googled for days now I cannot seem to find a straight answer to how to configure this with .NET 5 and Docker on Windows.

I was hoping it would be a matter of configuring a Dockerfile so that it was enabled during the build process but seems this is not how it works.

My Dockerfile looks like this:

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

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

FROM build AS publish
RUN dotnet publish "infuse-signalr-server.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "infuse-signalr-server.dll"]

and in my launchSettings.json file I have the following:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "https://localhost:44398",
      "sslPort": 44398
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "infuse_signalr_server": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": "true",
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "publishAllPorts": false,
      "useSSL": true
    }
  }
}

When I run my container it does open up my browser on the address https://localhost:44398/ as I expect it to, but of course the browser complains that there is no certificate.

What do I do?


Solution

  • If anyone else comes across this, here is what you do;

    Either in the same container as you run your application or a separate one that then communicates with each other over some sort of Ingress (or similar setup), make an nginx or apache server.

    Set up this server so that it acts as a reverse proxy. All incoming calls go to the nginx/apache server which then delegates those calls to the server application and back.