Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why automatic port forwarding? How to prevent VS Code from doing it?

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.

like image 998
Manfred Avatar asked Sep 07 '25 17:09

Manfred


2 Answers

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
      }
    }
  }
}
like image 98
Snow bunting Avatar answered Sep 10 '25 08:09

Snow bunting


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" }
}
like image 23
ikhvjs Avatar answered Sep 10 '25 06:09

ikhvjs