Chris Haines
Chris Haines

Reputation: 6555

How to get Visual Studio to launch correct url when using docker-compose with https

I've created a .net core 2.0 project and configured it to run over HTTPS, however I cannot get Visual Studio to launch the browser with the correct scheme/port when running in Docker debug mode.

The current behaviour is VS always launches on port 80 (HTTP), and I therefore have to manually change the url each time, which is cumbersome.

Program.cs

public class Program {
    public static void Main (string[] args) {
        BuildWebHost(args).Run ();
    }

    public static IWebHost BuildWebHost (string[] args) =>
        WebHost.CreateDefaultBuilder (args)
        .UseKestrel (options => {
            options.Listen (IPAddress.Any, GetPort(), listenOptions => {
                // todo: Change this for production
                listenOptions.UseHttps ("dev-cert.pfx", "idsrv3test");
            });
        })
        .UseStartup<Startup> ()
        .Build ();

    public static int GetPort() => int.Parse(Environment.GetEnvironmentVariable("Port") ?? "443");
}

Dockerfile

FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 443
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "MyApp.dll"]

docker-compose.override.yml

version: '3'

services:
  myapp:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - Port=443
    ports:
      - 443
networks:
  default:
    external:
      name: nat

Upvotes: 12

Views: 8005

Answers (2)

Rafał Cz.
Rafał Cz.

Reputation: 787

For anyone looking for code solution, what causing random port is this line in docker-compose.override.yml:

 ports:
  - "80"

Just remove it and add your port, for example:

 ports:
  - "8080:80"

Upvotes: 16

Chris Haines
Chris Haines

Reputation: 6555

Okay I have found out how to solve this myself.

Right-click the docker-compose project and go to properties.

There you can configure the Service URL that gets launched on run.

Upvotes: 23

Related Questions