Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach VS Code to a node process running in a docker container

I'm trying to attach the Visual Studio Code debugger to a node.js app that is running inside a Docker container.

I start the app like:

node --debug-brk app.js

I expose the debugger port in docker-compose.yml:

app:
  build: .
  working_dir: /code
  volumes:
    - .:/code
  command: npm run debug
  ports:
    - "3004:3000"
    - "5858:5858"

My launch.json looks like:

{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "Attach",
            "type": "node",
            "address": "localhost",
            "port": 5858
        }
    ]
}

Now, when I start the application and attach the debugger this will correctly connect (I can see the values flashing in the debugger UI already), but then it will stop, telling me the following:

Error opening 'app.js' (File not found: /code/app.js).

This is due to the fact that docker will not mount the app in root but in /code (see volumes in docker-compose.yml) and VS code is confused by the sudden offset.

When I run the application outside the container (i.e. locally, without offset) it works just as expected and I can use the debugger as expected.

There seems to be a cwd option for the launch configuration but I am not sure if that makes any difference in my case.

Can I fix this path offset? Am I missing something else here?

like image 545
m90 Avatar asked Oct 09 '15 07:10

m90


People also ask

How do you attach a process in VS Code?

Attach to a running processOpen the folder that contains the source files in Visual Studio Code. Click Run > Start Debugging. Select the configuration you want to use, if prompted. This creates a launch.

How do I attach a running docker container?

The docker exec and docker attach commands allow you to connect to a running container. To get an interactive shell to a container, use the exec command to start a new shell session. The attach command attaches your terminal to a running container.

Which docker command is used to attach to a running container?

Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.


1 Answers

This feature is now officially supported by VSCode: https://github.com/Microsoft/vscode-node-debug/issues/8

like image 142
m90 Avatar answered Nov 07 '22 13:11

m90