5

I'm using VSCode remote development to run and debug a django project inside a Docker container. In my devcontainer.json I forwarded the port 8000

 "forwardPorts": [8000],

and this is my launch.json

{

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/myapp/manage.py",
            "args": [
                "runserver",
                "0.0.0.0:8000"
            ],
            "django": true
        }
    ]
}

When I start the debug with such a configuration, I see 4 ports forwarded: port 8000 and other 3 random high ports

8000 -> localhost:8000 (the only one I'd expect to see)
34075 -> 127.0.0.1:34075
37301 -> 127.0.0.1:37301
42129 -> 127.0.0.1:42129

I'm wondering why those three ports are forwarded and how I can avoid it.

mic4ael
  • 7,974
  • 3
  • 29
  • 42
floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • 1
    I am wondering the same thing... I end up with hundreds of open ports due to debugging. – Zack Jan 16 '21 at 17:02
  • [Here](https://github.com/microsoft/vscode-remote-release/issues/4243#issuecomment-754079334) are some hints to avoid forwarding ports. I guess they will be opened anyhow into the dev container, though. – floatingpurr Jan 18 '21 at 13:27

1 Answers1

4

This is caused by the automatic port mapping behaviour of the ms-vscode-remote.remote-containers. See here: https://code.visualstudio.com/docs/remote/containers#_publishing-a-port

Publishing a port

Docker has the concept of "publishing" ports when the blah blah blah

To publish a port, you can:

Use the appPort property: blah blah

"appPort" property must be in devcontainer.json

  "appPort": [ 3000, "8921:5000" ]

Use the Docker Compose ports mapping: Blah Blah

They say that the "appPort" property is going to be mapped like we do on the docker-compose file. If you are running it with docker-compose file maybe the next code is also a solution. If you are not planning to use docker-compose then setting "appPort" property in devcontainer.json should be enough to avoid automatic mapping.

  ports:
  - "3000"
  - "8921:5000"

Then F1 to open dialog, rebuild devcontainer and happy hacking!

jgalan
  • 41
  • 3