This project shows how to configure your dev environment to be able to debug a NodeJs application deployed in a Docker container.
Install the following software in your dev machine:
- Visual Studio Code
- Docker
$ git clone https://github.com/riccardomerlin/docker-debug-nodejs
$ cd docker-debug-nodejs
$ docker build --tag docker-debug-nodejs:latest .
$ docker run --publish 3000:3000 docker-debug-nodejs:latest
Open a browser to http://localhost:3000
and the page should display
Hello Docker World!
.
-
Run the following command from the shell to start a new container instance with debug port (9222) exposed
$ docker run -p 3000:3000 -p 9222:9222 --entrypoint=/bin/bash docker-debug-nodejs:latest -c "npm run debug"
or alternatively
start the container using
docker-compose
$ docker-compose up
-
Open up /src/index.js in VS Code and set a break point at this line
res.end('Hello Docker World!')
-
Open the
Debug
panel and selectDocker: Attach to Node
configuration to attach the debugger. VS Code should display the debugger attached in the status bar at the very bottom of the window. -
Open the browser and go to
http://localhost:3000
, you will see the debugger in VS Code stopping in the break point set in the previous step.
Note: it works only when running the container with docker-compose
Edit the following line in /src/index.js
res.end('Hello Docker World!')
to
res.end('Hello Docker World [Edited]!')
and save the file.
In the terminal you should see node restarting and the debugger should automatically re-attach.
Go back to the browser and refresh the page, the debugger is still stopping in the same break-point.
Release the debugger and the page now displays
Hello Docker World [Edited]!
.
"remoteRoot"
, it has to specify the execution folder in the container in order to make the debugger able to set the break point correctly."restart"
, set it totrue
to supporthot changes
. This flag allows the debugger re-attaching after node is restarted.
volumes
, maps a folder in the host to a folder in the container (arguments separated by:
)ports
, maps container ports with host ports