9

I have used vs docker extension to create docker files. But I don't know what is the "proper" way to use docker-compose.debug.yml to debug my code, e.g. how do I set my env. so I can just hit F5 and all the magic happen.

I did work out a way to debug my code. First run docker-compose -f docker-compose.debug.yml in terminal. Then use the launch.json from In-container Node Development: Visual Studio Code to attach to my node in docker.

But I think Code may provide a simpler way to streamline the debug process.

2 Answers 2

6

You can do that but through some modifications.

launch.json

{
    // Use IntelliSense to learn about possible Node.js debug attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to Remote",
            "address": "127.0.0.1",
            "port": 9229,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "/usr/src/app",
            "preLaunchTask": "start_node_compose"
        }
        // {
        //     "type": "node",
        //     "request": "launch",
        //     "name": "Launch Program",
        //     "program": "${workspaceRoot}/index.js"
        // }
    ]
}

As you can see I commented the local launch and made this the first one so it is run on F5. Next we need to define a start_node_compose task

tasks.json

{
    "version": "0.1.0",
    "command": "myCommand",
    "isShellCommand": false,
    "args": [],
    "showOutput": "always",
    "tasks": [
        {
            "taskName": "start_node_compose",
            "showOutput": "always",
            "isBuildCommand": true,
            "command": "/bin/bash",
            "args": [
                "-c",
                "docker-compose -f docker-compose.yml -f docker-compose.debug.yml up -d && sleep 10"
            ]
        }
    ]
}

Then when you run the command using F5 you will be able to hit the breakpoint

Debug Breakpoint

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, if my docker-compose.yml has more than one services I don't know how to attach debugger the node service, how to set remoteRoot? Is it possible to do that? I raised an issue here github.com/Microsoft/vscode-recipes/issues/29
If you have more services, you will still have one node process or more? If there is just one node process then this would work
Hi, I have updated my comment at github. I did have one node, but I don't know how to make it work. Can you show me how to do that? Thanks!
Just change remoteRoot to /app in my config and it should work for you
Hi, I actually tried that but it didn't work. Same error message "Error: connect ECONNREFUSED 127.0.0.1:9229".
|
1

I had the same issue. Using --inspect=0.0.0.0:9229 solved it. I recommend you use --inspect-brk too, to have the node process wait for the debugger to attach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.