vscode-docker icon indicating copy to clipboard operation
vscode-docker copied to clipboard

Unable to navigate to https/443 after doing a Compose Up on default .net core webapi template

Open Konnor5092 opened this issue 6 years ago • 4 comments

Sorry if this question is slightly elementary. I'm new to all this.

When I do a Compose Up on my docker-compose.yml file, I can't browse the address that is mapped to port 443 when I click on the running container and choose Open in browser (e.g. https://localhost:32770/weatherForecast)

This is the log from the running container...

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

These were my setup steps

  • Create new .net core webapi project
  • Add dockerfile with Add Docker Files to Workspace which adds the following...
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

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

(Note at this point I can now run my application on both the 80 and 443 ports by using the Docker .NET Core Launch option in the debug options)

  • Add docker-compose.yml file by using the Add Docker Compose Files to Workspace which adds the following...
version: '3.4'

services:
  recipebook:
    image: recipebook
    build:
      context: .
      dockerfile: RecipeBook.Api/Dockerfile
    ports:
      - 80
      - 443
  • Right click on docker-compose.yml and click Compose Up
  • Right click on the running container and click Open in Browser
  • The https address is opened by default but fails to return data when browsing to https://localhost:32770/weatherForecast

Am I missing something here?

This query relates to step 5 in the .NET section of the docker-compose guidance - https://code.visualstudio.com/docs/containers/docker-compose. I was under the assumption I should be able to see my app working before attempting to attach the debugger.

I'm using docker for windows.

Many thanks

Konnor5092 avatar Jun 24 '20 10:06 Konnor5092

I was able to resolve this by creating the docker files for the project using Visual Studio, then editing the docker.commands.composeUp setting on the extension to account for both the docker-compose.yml and docker-compose.override.yml files required for running the project

Konnor5092 avatar Jun 24 '20 12:06 Konnor5092

Glad you got it working! By default, if you don't specifically right-click one of the compose files (but instead just open command palette -> "Compose Up"), it will use both docker-compose.yml + docker-compose.override.yml automatically; this should negate the need to modify docker.commands.composeUp.

bwateratmsft avatar Jun 24 '20 12:06 bwateratmsft

Thanks for this.

Is it expected that docker compose works out the box when adding it via the vs code extension for a .net core project - in terms of it working for both http and https? Or is that configuration the user is expected to do?

Konnor5092 avatar Jun 24 '20 14:06 Konnor5092

HTTP should work out of the box. HTTPS setup is significantly more complicated--

  1. The ASPNETCORE_URLS environment variable needs to be changed so that Kestrel listens on 443 with SSL enabled. For example, ASPNETCORE_URLS=https://+:443;http://+:80. Additionally, ASPNETCORE_ENVIRONMENT is set to Development.
    • Optionally, if you want the HTTP port to redirect to the HTTPS port, you set ASPNETCORE_HTTPS_PORT to the host-side HTTPS port. By default this port is unknown so this option isn't usable--Docker is allowed to choose a host-side port randomly--but if you use fixed ports, you can use this option.
  2. User secrets needs to be set up on the project. The built-in F5 experience for non-compose scenarios uses dotnet user-secrets init --project "path\to\project.csproj" --id "someRandomString". Then, a password is generated that Kestrel will use to access the SSL certificate created in the next step: dotnet user-secrets --project "path\to\project.csproj" set Kestrel:Certificates:Development:Password "randomStringForPassword". The whole user secrets path (%APPDATA%\Microsoft\UserSecrets) gets volume mapped into the container at /root/.microsoft/usersecrets.
  3. A certificate needs to be created and volume-mapped into the container. The built-in F5 experience for non-compose scenarios uses dotnet dev-certs to create and export this certificate, via roughly dotnet dev-certs https --trust -ep "%APPDATA%\ASP.NET\Https\projectName.pfx" -p "randomStringForPassword". The containing folder gets volume mapped into the container at '/root/.aspnet/https'.

HTTPS is much simpler in local debugging because Kestrel can directly use the ASP.NET Core development certificate on the host; it does not need to map anything anywhere and doesn't need user secrets to set a certificate password.

bwateratmsft avatar Jun 24 '20 15:06 bwateratmsft