It can be tricky to debug a NodeJS application if you haven’t worked with docker for long. Let me save you some time!
The dockerfile I am using:
- Create a
docker-compose.yaml
for main configuration:
2. Create a debug-compose.yaml
for debug configuration:
3. Add this configuration to your vscode launch.json
file:
4. Run:
docker-compose -f docker-compose.yaml -f debug-compose.yaml
Docker will combine docker-compose
and debug-compose
Overriding the first files configuration with the second. Check docker documentation for details.
5. Go to your vscode debug panel (CTRL + SHIFT + D) select Docker: Attach to Node
from the menu and click run, the debugger will break on first line. You can change line 12 of debug-compose.yaml
from --inspect-brk
to --inspect
to turn off breaking right away.
If you are not using docker-compose
. You can achieve the same thing by changing the the last line of Dockerfile
to CMD ["node", "--inspect=0.0.0.0", "server.js" ]
and running:
docker build .
docker run -p 9229:9229