microsoft / DockerTools

Tools For Docker, including Visual Studio Provisioning and Publishing
Other
175 stars 26 forks source link

Trouble binding to only 127.0.0.1 in launchSettings.json #420

Closed NoahSurprenant closed 2 months ago

NoahSurprenant commented 9 months ago

I am using .NET 6 and Microsoft.VisualStudio.Azure.Containers.Tools.Targets Version 1.19.6.

When launching from Visual Studio with my Docker profile I expect that I should be able to bind only to one IP address on my host machine rather than all. For example I would like to bind only to 127.0.0.1 so my application is only accessible to my machine. I have attempted setting my Docker profile in launchSettings.json like so:

"Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Docker"
      },
      "dockerfileRunArguments": "-p 127.0.0.1:44336:80 -p 127.0.0.1:44337:443",
      "httpPort": 0,
      "sslPort": 0,
      "publishAllPorts": false,
      "useSSL": false
    }

But this only seems to work if I remove applicationUrl and sslPort from my iisExpress settings. If I do not remove them then it binds to all network interfaces using the ports specified in iisExpress settings. I would not expect my iisExpress settings to have any affect on my docker profile. Also Visual Studio regenerates the properties after restarting, not to mention that me removing them means I cannot run in IIS Express configuration.

Is there another way to specify which IP address to bind to on the host, or at least specify localhost only?

dbreshears commented 9 months ago

I don't think there is a way to make this work currently. I'll log an internal backlog item for us to investigate.

NCarlsonMSFT commented 9 months ago

While our tools doesn't support it, you can globally configure Docker to use 127.0.0.1 by default and then -P will bind to it. https://docs.docker.com/network/packet-filtering-firewalls/#setting-the-default-bind-address-for-containers

dbreshears commented 2 months ago

Closing as there is a workaround available. If we get more upvotes we can re-open and consider.