maja
maja

Reputation: 797

Debug a Node app running inside a Docker container with VS Code

I'm trying to understand how I can debug a Node app running within a Docker container using the Docker plugin from VS Code.

To debug projects making use of more complex libraries/frameworks (Next.js for example) I need to make use of that command property, but I can't get the debugger to attach itself to the containerized app when using it.

I set up a basic Express app with an entrypoint script called index.js then I used the plugin tooling to generate these files:

launch.json:

{
  "configurations": [
    {
      "name": "Docker Node.js Launch",
      "type": "docker",
      "request": "launch",
      "preLaunchTask": "docker-run: debug",
      "platform": "node"
    }
  ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "node",
            "dockerBuild": {
                "dockerfile": "${workspaceFolder}/Dockerfile",
                "context": "${workspaceFolder}",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: release",
            "dependsOn": [
                "docker-build"
            ],
            "platform": "node"
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": [
                "docker-build"
            ],
            "dockerRun": {
                "env": {
                    "DEBUG": "*",
                    "NODE_ENV": "development"
                }
            },
            "node": {
                "enableDebugging": true
            }
        }
    ]
}

If I run the VS Code debugger with the profile registered in launch.json, the Docker image for the app is built, the container is launched and the debugger attaches itself to the process.

If I add the command property to the task.json file, like so:

package.json:

    ...
      "scripts": {
        "start": "node index.js",
    },
    ...

tasks.json:

    ...
    "dockerRun": {
        "command": "npm run start",
        "env": {
            "DEBUG": "*",
            "NODE_ENV": "development"
        }
    },
    ...

the image is built, the container is launched (I can use the app on a port set dynamically by Docker), but the debugger does not attach itself to the app process.

How can I debug a Node app running through a package manager inside a Docker container with VS Code?

Upvotes: -1

Views: 21

Answers (0)

Related Questions