This blog post shows how you can debug a simple Node.js application running in a Docker container. The tutorial is laid out in a fashion that allows you to use it as a reference while you’re building your own Node.js application and is intended for readers who have prior exposure to JavaScript programming and Docker.
Prerequisites
- Docker. For details about installing Docker, refer to the Install Docker page.
- Node.js 10 or higher. To check out if Node.js is installed on your computer, fire up a terminal window and type the following command:
node -vIf Node.js is already installed, you’ll see something like the following:
v10.15.3If Node.js is not installed, you can download the installer from the Download page.
- Microsoft Visual Studio. For details on how to install Visual Studio, refer to the Install Visual Studio page.
Initializing Your Todo Application
For the scope of this tutorial, we’ll create a bare-bones todo list that allows users to add and delete tasks. There will be a small bug in the application and we’ll use Visual Studio Code to debug the code and fix the issue. The knowledge you’ll acquire in this tutorial will help you debug your own applications. Let’s get started.
- Fire up a new terminal window, move into your projects directory, and then execute the following command:
mkdir MyTodoApp && cd MyTodoApp- Initialize the project with:
npm init -yThis will output something like the following:
Wrote to /Users/ProspectOne/Documents/MyTodoApp/package.json:{ "name": "MyTodoApp", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC"}Creating a Bare-bones Todo Application
We’ll build our todo application using Express, a fast, unopinionated, minimalist web framework for Node.js. Express was designed to make developing websites much easier and it’s one of the most popular Node.js web frameworks.
- Install
expressand a few other prerequisites by entering the following command:
npm install express body-parser cookie-session ejs --save> ejs@3.0.1 postinstall /Users/ProspectOne/Documents/test/MyTodoApp/node_modules/ejs> node ./postinstall.jsThank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)npm notice created a lockfile as package-lock.json. You should commit this file.npm WARN MyTodoApp@1.0.0 No descriptionnpm WARN MyTodoApp@1.0.0 No repository field.+ ejs@3.0.1+ body-parser@1.19.0+ express@4.17.1+ cookie-session@1.3.3added 55 packages from 39 contributors and audited 166 packages in 6.533sfound 0 vulnerabilities- Create a file called
app.jswith the following content:
const express = require('express')const app = express()const bodyParser = require('body-parser')const session = require('cookie-session')const urlencodedParser = bodyParser.urlencoded({ extended: false })const port = 3000app.use(session({ secret: process.env.SECRET })) .use(function (req, res, next) { next() }) .get ('/todo', function (req, res) { res.render('todo.ejs', { todolist: req.session.todolist }) }) .post ('/todo/add/', urlencodedParser, function (req, res) { if (req.body.newtodo != '') { req.session.todolist.push(req.body.newtodo) } res.redirect('/todo') }) .get ('/todo/delete/:id', function (req, res) { if (req.params.id != '') { req.session.todolist.splice(req.params.id, 1) } res.redirect('/todo') }) .use (function (req, res, next) { res.redirect('/todo') }) .listen(port, () => console.log(`MyTodo app is listening on port ${port}!`))Note that the above snippet is a derivative work of the code from the openclassroom.com website and explaining how this code works is beyond the scope of this tutorial. If the details are fuzzy, we recommend you check out their site to further your learning after you finish this tutorial.
- Create a file called
./views/todo.ejsand paste into it the following content:
<!DOCTYPE html><html> <head> <title>My todolist</title> <style> a {text-decoration: none; color: black;} </style> </head> <body> <h1>My todolist</h1> <ul> <% todolist.forEach(function(todo, index) { %> <li><a href="/todo/delete/<%= index %>">✘</a> <%= todo %></li> <% }); %> </ul> <form action="/todo/add/" method="post"> <p> <label for="newtodo">What should I do?</label> <input type="text" name="newtodo" id="newtodo" autofocus /> <input type="submit" /> </p> </form> </body></html>- At this point, your directory structure should look something like the following:
tree -L 2 -I node_modules.├── app.js├── package-lock.json├── package.json└── views └── todo.ejs1 directory, 4 files- Now you are ready to start your web server by entering:
SECRET=bestkeptsecret; node app.jsThis will print out the following message to the console:
MyTodo app is listening on port 3000!Create a Docker Image
Now that you’ve written the Todo application, it’s time to add create a Docker image for it. Each Docker container is based on a Docker image that contains all the information needed to deploy and run your app with Docker. To run a Docker container you can:
- Download an existing Docker image
- Create your own image
In this tutorial, you’ll create your own image. Note that a Docker image is usually comprised of multiple layers and each layer is basically a read-only filesystem. The way this works is that Docker creates a layer for each instruction found in the Dockerfile and places it atop of the previous layers. It is considered good practice to place the application’s code, that changes often, closer to the bottom of the file.
- Create a file called
Dockerfileand paste the following snippet into it:
FROM node:10WORKDIR /usr/src/appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 3000CMD [ "node", "app.js" ]Let’s take a closer look at this file:
- FROM: sets the base image. Everything you’ll add later on it’ll be based on this image. In this example, we’re using Node.js version 10.
- WORKDIR: this command sets the working directory that’ll be used for the COPY, RUN, and CMD commands.
- RUN: this line of code runs the
npm installcommand inside your Docker container. - COPY: copies files from the build context into the Docker image
- EXPOSE: specifies that a process running inside the container is listening to the 3000 port. This will be useful later in this tutorial when you’ll forward ports from the host to the container.
- CMD: this line runs the
node app.jsinside your Docker container only after the container has been started.
- To avoid sending large files to the build context and speed up the process, you can use a
.dockerignorefile. This is nothing more than a plain text file that contains the name of the files and directories that should be excluded from the build. You can think of it as something similar to a.gitignorefile. Create a file called.dockerignorewith the following content:
node_modulesnpm-debug.log- Now you can go ahead and build your Docker image by entering the
docker buildcommand followed by:
- The
-tparameter which specifies the name of the image - The path to the context which should point to the set of files you want to reference from your Dockerfile
docker build -t prospectone/my-todo-list .Sending build context to Docker daemon 24.58kBStep 1/7 : FROM node:10 ---> c5d0d6dc0b5bStep 2/7 : WORKDIR /usr/src/app ---> Using cache ---> 508b797a892eStep 3/7 : COPY package*.json ./ ---> 0b821f725c19Step 4/7 : RUN npm install ---> Running in d692a6278d2b> ejs@3.0.1 postinstall /usr/src/app/node_modules/ejs> node ./postinstall.jsThank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)npm WARN MyTodoApp@1.0.0 No descriptionnpm WARN MyTodoApp@1.0.0 No repository field.added 55 packages from 39 contributors and audited 166 packages in 2.564sfound 0 vulnerabilitiesRemoving intermediate container d692a6278d2b ---> 067de030e269Step 5/7 : COPY . . ---> 3141ccb6e094Step 6/7 : EXPOSE 3000 ---> Running in eb824e38d8c6Removing intermediate container eb824e38d8c6 ---> b09d55adc1c4Step 7/7 : CMD [ "node", "app.js" ] ---> Running in 7e77e0cbfa75Removing intermediate container 7e77e0cbfa75 ---> c0a2db4c7a65Successfully built c0a2db4c7a65Successfully tagged prospectone/my-todo-list:latestAs mentioned above, the way the docker build command works is that it adds a new layer for each command in your Dockerfile. Then, once a command is successfully executed, Docker deletes the intermediate container.
- Now that you’ve built your image, let’s run it by entering the
docker runcommand and passing it the following arguments:
-pwith the port on the host (3001) that’ll be forwarded to the container (3000), separated by:-eto create an environment variable calledSECRETand set its value tobestkeptsecret-dto specify that the container should be run in the background- The name of the image (
prospectone/my-awesome-app)
docker run -p 3001:3000 -e SECRET=bestkeptsecret -d prospectone/my-todo-listdb16ed662e8a3e0a93f226ab873199713936bd687a4546d2fce93e678d131243- You can verify that your Docker container is running with:
docker psThe output should be similar to:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa6eb166191c7 prospectone/my-todo-list "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3001->3000/tcp happy_hawking- To inspect the logs, enter the
docker logscommand followed by theidof your container:
docker logs a6eb166191c7MyTodo app is listening on port 3000!- Now that your application is up and running, point your browser to
http://localhost:3001
and let us add a new todo. As you can see below, the application errors out on line 15 of thetodo.ejsfile:

In the next sections, you’ll learn how to debug this using Visual Studio Code.
- But first, stop the container with:
docker kill a6eb166191c7a6eb166191c7Enable Debugging in Microsoft Visual Studio Code
Visual Studio Code provides debugging support for the Node.js applications running inside a Docker container. Follow the next steps to enable this feature:
- Edit your Dockerfile by replacing the following line:
CMD [ "node", "app.js" ]with:
CMD [ "npm", "run", "start-debug" ]Your Dockerfile should look something like the following:
FROM node:10WORKDIR /usr/src/appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 3000CMD [ "npm", "run", "start-debug" ]- Open the
package.jsonfile and add the following line to thescriptsobject:
"start-debug": "node --inspect=0.0.0.0 app.js"This line of code starts the Node.js process and listens for a debugging client on port 9229.
Here’s how your package.json file should look like:
{ "name": "MyTodoApp", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start-debug": "node --inspect=0.0.0.0 app.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "body-parser": "^1.19.0", "cookie-session": "^1.3.3", "ejs": "^3.0.1", "express": "^4.17.1" }}- Every time the Dockerfile gets updated, you must build again your Docker image:
docker build -t prospectone/my-todo-list .Sending build context to Docker daemon 19.97kBStep 1/7 : FROM node:10 ---> c5d0d6dc0b5bStep 2/7 : WORKDIR /usr/src/app ---> Using cache ---> 508b797a892eStep 3/7 : COPY package*.json ./ ---> c0eec534b176Step 4/7 : RUN npm install ---> Running in a155901cb957npm WARN MyAwesomeApp@1.0.0 No descriptionnpm WARN MyAwesomeApp@1.0.0 No repository field.added 50 packages from 37 contributors and audited 126 packages in 11.504sfound 0 vulnerabilitiesRemoving intermediate container a155901cb957 ---> 010473a35e41Step 5/7 : COPY . . ---> 76dfa12d4db4Step 6/7 : EXPOSE 3000 ---> Running in b5a334c9a2eaRemoving intermediate container b5a334c9a2ea ---> b5a869ab5441Step 7/7 : CMD [ "npm", "run", "start-debug" ] ---> Running in 1beb2ca9a391Removing intermediate container 1beb2ca9a391 ---> 157b7d4cb77bSuccessfully built 157b7d4cb77bSuccessfully tagged prospectone/my-todo-list:latestNote that the Step 7 has been updated, meaning that Docker will now execute the npm run start-debug command.
- To enable debugging with Visual Studio Code, you must also forward port
9229. Start your Docker container by entering:
docker run -p 3001:3000 -p 9229:9229 -e SECRET=bestkeptsecret22222 -d perfops/my-todo-list0f5860bebdb5c70538bcdd10ddc901411b37ea0c7d92283310700085b1b8ddc5- You can inspect the logs by entering the
docker logscommand followed theidof your container:
docker logs 0f5860bebdb5c70538bcdd10ddc901411b37ea0c7d92283310700085b1b8ddc5> My@1.0.0 start-debug /usr/src/app> node --inspect=0.0.0.0 app.jsDebugger listening on ws://0.0.0.0:9229/59d4550c-fc0e-412e-870a-c02b4a6dcd0fFor help, see: https://nodejs.org/en/docs/inspectorNote that the debugger is now listening to port 9229. Next, you’ll configure Visual Studio code to debug your application.
Debug Your Application with Visual Studio Code
- In Visual Studio Code, open the
MyTodoAppdirectory.

- The configuration for debugging is stored in a file called
launch.json. To open it, pressCommand+Shift+Pand then chooseDebug: Open launch.json.

- Replace the content of the
launch.jsonfile with the following snippet:
{ "version": "0.2.0", "configurations": [ { "name": "Docker: Attach to Node", "type": "node", "request": "attach", "port": 9229, "address": "localhost", "localRoot": "${workspaceFolder}", "remoteRoot": "/usr/src/app", "protocol": "inspector", "skipFiles": [ "${workspaceFolder}/node_modules/**/*.js", "<node_internals>/**/*.js" ] } ]}Note that we’re using the skipFiles attribute to avoid stepping through the code in the node_modules directory and the built-in core modules of Node.js.
- Now everything is set up and you can start debugging your application. Remember that there was an error at line 15 in the
views.jsfile, which basically iterates over thetodolistarray:todolist.forEach(function(todo, index). Looking at theapp.jsfile you’ll see that todo.ejs gets rendered at line 14. Let’s add a breakpoint so we can inspect the value of thetodolistvariable:

- Enter
Shift+Command+Dto switch to theDebugview. Then, click theDebug and Runbutton:

- To inspect the value of the
req.session.todolistvariable, you must add a new expression to watch by selecting the+sign and then typing the name of the variable (req.session.todolist):

- Switch to the browser window and reload the
http://localhost:3001
page.

Note the Waiting for localhost message at the bottom. This means that our breakpoint has paused execution and we can inspect the value of the req.session.todolist variable. Move back to Visual Studio to get details:

So the req.session.todolist variable is undefined. Can you think of how you could fix this bug? The answer is below, but don’t continue until you’ve given it some thought.
- The
ejbtemplate iterates over thetodolistarray which should be stored in the current session. But we forgot to initialize this array so it’sundefined. Let’s fix that by adding the following lines of code to the.usefunction :
if (typeof (req.session.todolist) == 'undefined') { req.session.todolist = []}Make sure you paste this snippet just above the line of code that calls the next function. Your .use function should look like below:
app.use(session({ secret: process.env.SECRET })) .use(function (req, res, next) { if (typeof (req.session.todolist) == 'undefined') { req.session.todolist = [] } next() })- Retrieve the
idof your running container :
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScb9f175f7af3 prospectone/my-todo-list "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:9229->9229/tcp, 0.0.0.0:3001->3000/tcp nervous_hopper- Stop the container by entering the
docker killcommand followed by itsid:
docker kill cb9f175f7af3cb9f175f7af3- To apply the changes you must run the
docker buildcommand again:
docker build -t prospectone/my-todo-list .Sending build context to Docker daemon 26.11kBStep 1/7 : FROM node:10 ---> c5d0d6dc0b5bStep 2/7 : WORKDIR /usr/src/app ---> Using cache ---> 508b797a892eStep 3/7 : COPY package*.json ./ ---> Using cache ---> c5ac875da76bStep 4/7 : RUN npm install ---> Using cache ---> 29e7b3bac403Step 5/7 : COPY . . ---> b92f577afd57Step 6/7 : EXPOSE 3000 ---> Running in 78606a3c2e03Removing intermediate container 78606a3c2e03 ---> 59c2ed552549Step 7/7 : CMD [ "npm", "run", "start-debug" ] ---> Running in e0313973bb5aRemoving intermediate container e0313973bb5a ---> 70a675646c0dSuccessfully built 70a675646c0dSuccessfully tagged prospectone/my-todo-list:latest- Now you can run the container with:
docker run -p 3001:3000 -p 9229:9229 -e SECRET=bestkeptsecret222212 -d prospectone/my-todo-listf75d4ef8b702df13749b10615f3945ea61b36571b0dc42b76f50b3c99e14f4c6- Inspect the logs by running the following command:
docker logs 10f467dbb476f75d4ef8b702df13749b10615f3945ea61b36571b0dc42b76f50b3c99e14f4c6- Reload the page and add a new task:

Congratulations, you’ve successfully written a bare-bones todo app, ran it inside a Docker container, and used Visual Studio Code to debug it and fix a bug. In the next blog post, we’ll walk you through the process of dockerizing an existing application.
Related articles
Subscribe to our newsletter
Get the latest industry trends, exclusive insights, and Gcore updates delivered straight to your inbox.






