Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Debug Saral backend running on Docker Container in VS Code

Dileep Gadiraju edited this page Sep 20, 2021 · 3 revisions

Add debug with inspect option in scripts section of package.json file as below.

  "name": "saral-backend",
  "version": "1.0.0",
  "description": "Saral Backend APIs",
  "main": "index.js",
  "scripts": {
    "start": "env-cmd ./config/prod.env pm2 start src/index.js",
    "dev": "env-cmd ./config/dev.env nodemon --inspect=0.0.0.0:9229  src/index.js -e js,pug",
    "uat": "env-cmd ./config/uat.env nodemon --inspect=0.0.0.0:9229 src/index.js  -e js,pug",
    "prod": "env-cmd ./config/prod.env nodemon --inspect=0.0.0.0:9229 src/index.js  -e js,pug",
    "debug": "env-cmd ./config/debug.env nodemon --inspect=0.0.0.0:9229  src/index.js -e js,pug",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "bluebird": "^3.7.2",
    "dotenv": "^10.0.0",
    "exceljs": "^4.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.20",
    "mongodb": "^3.6.3",
    "mongoose": "^5.11.5",
    "pug": "^3.0.2",
    "uuidv1": "^1.6.14",
    "validator": "^13.5.2"
  },
  "devDependencies": {
    "env-cmd": "^8.0.2",
    "nodemon": "^2.0.6"
  }
}

Build the docker image with the above package.json file change

docker build . -t saral-backend:1.0-latest

Add below launch.json configuration in VS Code

launch.json

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

Bring up backend container with debug port enabled using docker-compose

Make sure docker-compose.yml has debug port 9229 enabled.

docker-compose up -d

docker-compose.yml

# Maintainer [email protected]
version: '3.3'
services:
  saral-backend:
    image: saral-backend:1.0-latest
    container_name: saral-backend
    restart: always
    networks: 
      - saralbackend-network
    ports:
      - "0.0.0.0:3005:3005"
      - "0.0.0.0:9229:9229"
    depends_on:
      - saral-mongodb
    links:
      - saral-mongodb
    environment:
      PROFILE: debug
      PORT: 3005
    #MONGODB_URL: mongodb://docker.for.mac.localhost:27019/local
      MONGODB_URL: mongodb://saral-mongodb:27017/saralv1db
      JWT_SECRET: SARALDATA_NODE
  saral-mongodb:
    image: mongo:latest
    container_name: saral-mongodb
    restart: unless-stopped
    networks: 
      - saralbackend-network
    #environment:
      # MONGO_INITDB_ROOT_USERNAME: admin
      # MONGO_INITDB_ROOT_PASSWORD: admin
      # MONGO_INITDB_DATABASE: saralv1dev
    ports:
      - "0.0.0.0:27019:27017"
    volumes:
      - /usr/local/mongodb/data/saralv1db:/data/db

networks:
  saralbackend-network:
    driver: bridge

From RUN AND DEBUG run Docker: Attach to Node

Add break points to scripts that appear in LOADED SCRIPTS pane in the editor.