3 steps to debug your Docker container in VSCode

3 steps to debug your Docker container in VSCode

I haven't seen yet a simple 3-step guide for when you have a Node app with multiple services so here it is.

1 - Configure your package.json script to enable port watching on all services

Add the inspect flag to your node call in the package.json script.

The --inspect flag is what tells Node.js process to listen for a debugging client. Nodemon makes the node app automatically restart when you change the source code but this will work with plain Node too.

No alt text provided for this image

2 - Configure your Docker Compose YAML file to open ports

Edit your docker-compose.yaml to add port 9229 under your app service configuration. If you have more than one service, use a different port for each: 9229, 9230, and so forth.

No alt text provided for this image

3- Create the folder .vscode with a launch.json file containing the debugging configuration

You can either create the file manually or follow these steps to create a launch.json file. If your app has more than one service, you will have to have one configuration for each service and the port has to match (9229, 9230, ...).

No alt text provided for this image

Launch Debug

Once you've started Docker and built the app, you can open a file on the debug tab, set breakpoints and click the green arrow to start debugging.

No alt text provided for this image

Further reading:

要查看或添加评论,请登录

社区洞察

其他会员也浏览了