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?
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.
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.
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.
This feature is now officially supported by VSCode: https://github.com/Microsoft/vscode-node-debug/issues/8
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