18

I am developing inside a docker container using the VSCode Remote Containers extension. I start a server inside the container which listens at port 3342 and would like to access the webpage using other devices in the same network.

I forward port 3342 inside the Ports tab and I am able to open the webpage in my host OS using localhost:3342 and 127.0.0.1:3342, but failed when using my local IP address 10.10.11.90:3342.

I checked the listening ports in my host OS:

> sudo lsof -nP -iTCP:3342 | grep LISTEN
Code\x20H 4181 alvin   27u  IPv4 0x65c113c13860c1b7      0t0  TCP 127.0.0.1:3342 (LISTEN)

It seems that VSCode is only listening at 127.0.0.1.

Is there a way for me to tell VSCode to listen at 0.0.0.0 so that it accepts requests from all network interface?


My configuration:

  • OS: macOS 10.15.7
  • VSCode: 1.57.0
  • VSCode Remote Containers extension: 0.183.0
Alvin Leung
  • 668
  • 1
  • 5
  • 10

4 Answers4

33

In VSCode settings, set to "All Interfaces" here:

Remote: Local Port Host

allInterfaces

If it's set to "localhost", devContainers forwarded ports will only listen to 127.0.0.1.

When set to "allInterfaces", it will listen to 0.0.0.0.

See screenshot for context: enter image description here

Sy.Yah
  • 25
  • 6
Olivier Latorre
  • 346
  • 3
  • 5
6

For VSCode: 1.69.0 on Ubuntu, the forward setting page is not available.

It is verified that we can do allinterface port forwarding by modifying the devcontainer.json by adding in the following parameters.

"appPort": ["8107:8107"],

After relaunching your container, the port forwarding can be verified in the "port" tab of the tool bar.

enter image description here

You can find the official reference of appPort parameter.

Chris
  • 6,914
  • 5
  • 54
  • 80
HarryQ
  • 1,281
  • 2
  • 10
  • 25
2

Update to Ubuntu comment made earlier: If you don't change the Remote: Local Port Host setting then vscode will only listen on 127.0.0.1 and not on its public ip address

sudo lsof -nP -iTCP:5432 | grep LISTEN
code    76068 brent   42u  IPv4 1089447      0t0  TCP 127.0.0.1:5432 (LISTEN)

Remote: forward setting

  • Manage settings and type forward
  • go to the remote extension settings
  • change Remote: Local Port Host from localhost to all interfaces. As of vscode 1.79.2 vscode has the Remote: Local Port Host setting when running on Ubuntu 22.04 and the "appPort": ["5432:5432"] setting is not allowed in the devcontainer.json file. After the setting is changed in vscode and the container is rebuilt vscode will listen on all interfaces not just 127.0.0.1
sudo lsof -nP -iTCP:5432 | grep LISTEN
code    76068 brent   42u  IPv4 1216443      0t0  TCP *:5432 (LISTEN)
brent groves
  • 71
  • 1
  • 5
0

If you are in Linux your can also use

    "runArgs": [
        // Use the network stack of the host machine
        "--net=host"
    ]

NOT SUPPORTED ON MAC

User12547645
  • 6,955
  • 3
  • 38
  • 69