DEV Community

ReMa
ReMa

Posted on

Debugging Nest.js (TypeScript) in a Docker Container

Debugging Nest.js in a Docker Container

This recipe shows how to run and debug a VS Code Nest.js, Full Stack TypeScript Framework, project in a Docker container.

The recipe assumes that you have a recent version of Docker installed.

Here is repository.

Step 1. Create a New App

With the following commands, create the directory (nest-js-app) and files for the app.

$ mkdir nest-js-app && cd nest-js-app
$ touch Dockerfile docker-compose.yml .dockerignore .gitignore

The app directory structure should be:

- nest-js-app
    - .dockerignore
    - .gitignore
    - docker-compose.yml
    - Dockerfile

Add the following to the Dockerfile file:

FROM node:8.10.0

RUN mkdir -p /nest
ADD . /nest
WORKDIR /nest

RUN npm i -g @nestjs/cli

Add the following to the docker-compose.yml file:

version: "3"

services:
  nest:
    build: .
    volumes:
      - .:/nest
    ports:
      - 3000:3000
      - 9229:9229
    tty: true

Add the following to the .gitignore file:

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Coverage directory used by tools like istanbul
coverage

# Dependency directories
node_modules/

# Output files
dist

# dotenv environment variables file
.env

Add the following to the .dockerignore file:

.git
.github
.vscode
coverage
docker-compose.yml
README.md

# Node Files #
node_modules
npm-debug.log
npm-debug.log.*

Build docker image:

$ docker-compose build

Start and login to the container:

$ docker-compose up -d
$ docker-compose exec nest bash

Scaffold the base project with the Nest CLI and install dependencies:

# nest new .
# npm install

Run the app:

# npm start

Open a browser and navigate to http://localhost:3000.
You should see the Hello world! message.

Step 2. Set up the debugging environment

With the following commands, create the directory (.vscode) and files for debugging.

# mkdir .vscode && touch .vscode/launch.json nodemon-docker-debug.json

The app directory structure should be:

スクリーンショット 2018-11-11 5.03.17.png

Add the following to the launch.json file:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Docker: Attach to Node",
      "type": "node",
      "request": "attach",
      "port": 9229,
      "address": "localhost",
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/nest",
      "protocol": "inspector",
      "restart": true
    }
  ]
}

Add the following to the nodemon-docker-debug.json file:

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "tsc && node --inspect=0.0.0.0 ./dist/main.js"
}

Add the following line into the scripts block of the package.json file:

"scripts": {
  "debug": "nodemon -L --config nodemon-docker-debug.json",

}

Step 3. Debugging

For the test operation, add some codes into the src/app.service.ts and set breakpoints as below.

スクリーンショット 2018-11-11 4.39.50.png

Run the app in debugging mode:

# npm run debug

スクリーンショット 2018-11-11 11.52.41.png

Start the debug on the VScode.

スクリーンショット 2018-11-11 4.44.01.png

Open a browser and navigate to http://localhost:3000.
You should see the program stop at the breakpoints.

スクリーンショット 2018-11-11 4.52.37.png

Happy coding!

Reference

Debugging TypeScript in a Docker Container
nodemon
Is there a way to use npm scripts to run tsc -watch && nodemon --watch?

Top comments (2)

Collapse
 
basicbrogrammer profile image
Jeremy Ward 😎🤓

I'm gonna have to give this a shot ... I'm running into an issue now where my docker compose file is running yarn start:dev which runs nodemon and the app crashes on a file change. I tried to change start:dev to nodemon -L but that didn't fix it :/ Any thoughts?

Collapse
 
basicbrogrammer profile image
Jeremy Ward 😎🤓

I was able to figure it out 1) upgrade nodemon 2) change my base image to node:10.14 instead of node:10.14-alpine