7

My setup is VS Code with a dev container. When I start an application that produces output that looks like a URL, VS Code will extract the port number and automatically forward that port. This process is described in some detail here.

My question is twofold:

  1. What is the advantage of automatically forwarding ports?

  2. How do I effectively prevent VS Code from automatically forwarding any port?

Solutions I considered and tested include using settings such as:

{
   // Prevent VS Code's automatic port forwarding
   "remote.autoForwardPorts": false,
   "remote.restoreForwardedPorts": false,
   "remote.portsAttributes": {
      "1-65000": {
         "label": "Application",
         "onAutoForward": "ignore"
      }
   },
}

I have experimented with these settings at the user level, at the dev container level (aka remote), and at the workspace level. Furthermore, I've tried these settings in devcontainer.json as well. I've also rebuilt the dev container to ensure settings were applied.

However, I didn't have much luck with any of this. VS Code keeps automatically forwarding ports. I'm looking for a durable solution.

Manfred
  • 5,320
  • 3
  • 35
  • 29
  • Did you try this [devcontainer.json](https://github.com/microsoft/vscode/issues/108346#issuecomment-723134100) and rebuild? – ikhvjs Sep 16 '22 at 21:40
  • 1
    @ikhvjs Yes, I rebuilt the dev container a few times along the way, too, but am not able to get a reliable consistent result. VS Code keeps forwarding ports. – Manfred Sep 17 '22 at 05:04
  • What OS are you using? If you are using Windows, the source codes are in Windows filesystem or in WSL filesystem? – ikhvjs Sep 17 '22 at 18:03
  • The dev container runs on Linux, VS Code frontend on Windows. I'm aware where to find the settings. I've updated them in four different places: user, workspace, dev container, and also in `devcontainer.json`. I have some dev containers where it works and some where it doesn't. I am looking for the determining factor, ideally other than a bug in VS Code. – Manfred Sep 22 '22 at 06:42
  • @ikvvjs I tried that setting in [devcontainer.json](https://github.com/microsoft/vscode/issues/108346#issuecomment-723134100) but not change in behavior. – Manfred Oct 23 '22 at 22:19

2 Answers2

2

I check with the devcontainer.json reference documentation and port attributes.

I think you need the below settings in the devcontainer.json

otherPortsAttributes: Default options for ports, port ranges, and hosts that aren’t configured using portsAttributes

onAutoForward: A value of ignore means that this port should not be auto-forwarded at all.

{
  "otherPortsAttributes": { "onAutoForward" : "ignore" }
}
ikhvjs
  • 5,316
  • 2
  • 13
  • 36
2

I added "remote.autoForwardPorts": false in the .devcontainer/devcontainer.json. Currently the right place seems to be the following:

{
  "customizations": {
    "vscode": {
      "settings": {
        "remote.autoForwardPorts": false
      }
    }
  }
}
Snow bunting
  • 1,120
  • 8
  • 28