dockernext.jsweb-inspector

how to enable "--inspect=0.0.0.0:9229" in nextjs when running in docker to take memory dump shapshot


How do add --inspect=0.0.0.0:9229 when using nextjs running in a Docker. Currently in docker file the app is started

# Start the app
CMD ["npm", "start"]

in package.json:

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },

Where do I add --inspect=0.0.0.0:9229 so that I can take a memory dump snapshot.


Solution

  • 🚨 You need to share port 9229 from your container, so don't forget to supply -p 9229:9229 when you docker run.

    Option 1: Dockerfile

    One option would be to put it into your Dockerfile (see simplified version below):

    FROM node:18.17.0-alpine as base
    
    WORKDIR /app
    
    COPY next.config.js package.json .
    COPY pages/ ./pages
    
    RUN npm install --frozen-lockfile
    RUN npm run build
    
    ENV NODE_OPTIONS="--inspect=0.0.0.0:9229"
    
    CMD npm start
    

    Option 2: Command Line

    Alternatively you can supply the environment variable on the command line when you docker run like this:

    docker run -e NODE_OPTIONS="--inspect=0.0.0.0:9229" -p 9229:9229 <other arguments>