Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 596
Chris Haines Avatar asked Dec 04 '17 17:12

Chris Haines


People also ask

How do I run a docker compose in Visual Studio?

In the Web API project, again right-click on the project node, and choose Add > Container Orchestrator Support. Choose Docker Compose, and then select the same target OS. In this step, Visual Studio will offer to create a Dockerfile.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


2 Answers

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"
like image 177
Rafał Cz. Avatar answered Oct 05 '22 16:10

Rafał Cz.


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.

like image 44
Chris Haines Avatar answered Oct 05 '22 17:10

Chris Haines