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:
What is the advantage of automatically forwarding ports?
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.
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
}
}
}
}
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 usingportsAttributes
onAutoForward
: A value ofignore
means that this port should not be auto-forwarded at all.
{
"otherPortsAttributes": { "onAutoForward" : "ignore" }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With