HO与DLV和MODD在Docker上进行调试

发布于 2025-01-19 02:29:49 字数 1106 浏览 1 评论 0原文

我正在 Docker 上运行 Go 应用程序,并希望使用 VSCode 通过 DLV 对其进行调试同时使用 MODD 进行应用程序重建。到目前为止我不知道如何连接到调试器。

Docker:

FROM golang:1.18 as dev
WORKDIR /root
RUN GO111MODULE=on go install github.com/cortesi/modd/cmd/modd@latest
RUN go install github.com/go-delve/delve/cmd/dlv@latest
COPY . .
CMD modd

MODD:

**/*.go !**/*_test.go {
    prep: go build -o app main.go
    prep: dlv exec --headless --continue --listen localhost:2345 --accept-multiclient ./app
    daemon +sigterm: ./app
}

DOCKER_COMPOSE (暴露端口):

ports:
      - "5000:5000"
      - "2345:2345"

VSCode 配置:

{
        "name": "Connect to Go server",
        "type": "go",
        "request": "attach",
        "mode": "remote",
        "remotePath": "${workspaceFolder}",
        "port": 2345,
        "host": "127.0.0.1",
    }

问:如何使用 MODD 让 DLV 在 Docker 上工作?

谢谢!

I'm running a Go app on the Docker and want to use VSCode to debug it via DLV at the same time using MODD for app rebuild. So far I cannot figure out how to connect to the debugger.

Docker:

FROM golang:1.18 as dev
WORKDIR /root
RUN GO111MODULE=on go install github.com/cortesi/modd/cmd/modd@latest
RUN go install github.com/go-delve/delve/cmd/dlv@latest
COPY . .
CMD modd

MODD:

**/*.go !**/*_test.go {
    prep: go build -o app main.go
    prep: dlv exec --headless --continue --listen localhost:2345 --accept-multiclient ./app
    daemon +sigterm: ./app
}

DOCKER_COMPOSE (expose port):

ports:
      - "5000:5000"
      - "2345:2345"

VSCode configuration:

{
        "name": "Connect to Go server",
        "type": "go",
        "request": "attach",
        "mode": "remote",
        "remotePath": "${workspaceFolder}",
        "port": 2345,
        "host": "127.0.0.1",
    }

Q: How to make DLV work on Docker with MODD?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2025-01-26 02:29:50

好吧,据我所知,做你想做的事情并不简单,因为你必须观察主机中文件的更改才能触发容器内的 dlv,从而扰乱 vscode 正在进行的调试会话。

这是设置 vscode 来调试容器中的应用程序并使用 modd 在文件更改时在容器内重新启动调试的hacky方法。

(确保您的主机中安装了 modd)

dlv+docker+modd

.vscode/launch.json

 {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/root",
            "port": 2345,
            "host": "127.0.0.1",
            "trace": "verbose",
            "preLaunchTask": "setup docker debug",
            "postDebugTask": "teardown docker debug"
        }
    ]
 }

.vscode/tasks.json - 此文件将指示 vscode 通过以下方式运行容器docker compose 并在后台运行 modd

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "setup docker debug",
            "dependsOn": [               
                "show app console"                
            ]
        },
        {
            "label": "teardown docker debug",
            "dependsOrder": "sequence",
            "dependsOn": [
                "stop all containers"
            ]
        },
        {
            "label": "show app console",
            "command": "docker logs app --follow",
            "type": "shell",
            "isBackground": true,
            "presentation": {
                "reveal": "always",
                "panel": "dedicated",
                "clear": true,
                "showReuseMessage": true
            },
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": ".",
                            "file": 1,
                            "location": 2,
                            "message": 3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": ".",
                        "endsPattern": ".",
                    }
                }
            ],
            "dependsOn":[
                "start all containers",
                "modd"
            ]
        },
        {
            "label": "start all containers",
            "type": "shell",
            "command": "docker-compose up --build --force-recreate --detach",
            "presentation": {
                "reveal": "always",
                "panel": "shared",
                "clear": true,
                "showReuseMessage": true
            },
            "dependsOn":[
                "stop all containers"
            ]
        },
        {
            "label": "stop all containers",
            "type": "shell",
            "command": "docker-compose down",
            "presentation": {
                "panel": "shared",
                "clear": true
            },
        },
        {
            "label": "modd",
            "type": "shell",
            "isBackground": true,
            "command": "modd",
            "presentation": {
                "panel": "new", 
                "clear": true
            },
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": ".",
                            "file": 1,
                            "location": 2,
                            "message": 3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": ".",
                        "endsPattern": ".",
                    }
                }
            ],
        }
    ]
}

docker-compose.yml

version: "3.8"
services:
  app:
    container_name: app
    build:
      context: .
    restart: on-failure
    ports:
      - 5000:5000
      - 2345:2345
    security_opt:
      - apparmor=unconfined
    cap_add:
      - SYS_PTRACE

Dockerfile

FROM golang
WORKDIR /root
RUN go install github.com/go-delve/delve/cmd/dlv@latest
COPY . .
ENTRYPOINT dlv --listen=:2345 --api-version=2 --headless --accept-multiclient debug .

dlv.txt - 此文件将用于调用 dlv 来重建并继续

rebuild
continue

modd.conf - modd 会将所有文件复制回容器并发出重建并继续命令到正在运行的 dlv

**/*.go !**/*_test.go {
    prep +onchange: docker cp ./ app:/root/
    prep +onchange: timeout 1 dlv connect localhost:2345 --init dlv.txt
}

您将能够设置断点和所有内容,但您会注意到有时您需要手动暂停并继续恢复调试会话。

Well, afaik it is not trivial to do what you want, because you have to watch files being changed in your host to trigger dlv inside the container, messing up with vscode's ongoing debug session.

Here is hacky way to setup vscode to debug app in container and use modd to restart debug inside the container on file change.

(Make sure you have modd installed in your host machine)

dlv+docker+modd

.vscode/launch.json

 {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/root",
            "port": 2345,
            "host": "127.0.0.1",
            "trace": "verbose",
            "preLaunchTask": "setup docker debug",
            "postDebugTask": "teardown docker debug"
        }
    ]
 }

.vscode/tasks.json - This file will instruct vscode to run your container via docker compose and to run modd in background

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "setup docker debug",
            "dependsOn": [               
                "show app console"                
            ]
        },
        {
            "label": "teardown docker debug",
            "dependsOrder": "sequence",
            "dependsOn": [
                "stop all containers"
            ]
        },
        {
            "label": "show app console",
            "command": "docker logs app --follow",
            "type": "shell",
            "isBackground": true,
            "presentation": {
                "reveal": "always",
                "panel": "dedicated",
                "clear": true,
                "showReuseMessage": true
            },
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": ".",
                            "file": 1,
                            "location": 2,
                            "message": 3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": ".",
                        "endsPattern": ".",
                    }
                }
            ],
            "dependsOn":[
                "start all containers",
                "modd"
            ]
        },
        {
            "label": "start all containers",
            "type": "shell",
            "command": "docker-compose up --build --force-recreate --detach",
            "presentation": {
                "reveal": "always",
                "panel": "shared",
                "clear": true,
                "showReuseMessage": true
            },
            "dependsOn":[
                "stop all containers"
            ]
        },
        {
            "label": "stop all containers",
            "type": "shell",
            "command": "docker-compose down",
            "presentation": {
                "panel": "shared",
                "clear": true
            },
        },
        {
            "label": "modd",
            "type": "shell",
            "isBackground": true,
            "command": "modd",
            "presentation": {
                "panel": "new", 
                "clear": true
            },
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": ".",
                            "file": 1,
                            "location": 2,
                            "message": 3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": ".",
                        "endsPattern": ".",
                    }
                }
            ],
        }
    ]
}

docker-compose.yml

version: "3.8"
services:
  app:
    container_name: app
    build:
      context: .
    restart: on-failure
    ports:
      - 5000:5000
      - 2345:2345
    security_opt:
      - apparmor=unconfined
    cap_add:
      - SYS_PTRACE

Dockerfile

FROM golang
WORKDIR /root
RUN go install github.com/go-delve/delve/cmd/dlv@latest
COPY . .
ENTRYPOINT dlv --listen=:2345 --api-version=2 --headless --accept-multiclient debug .

dlv.txt - This file will be used to call dlv to rebuild and continue

rebuild
continue

modd.conf - modd will copy all files back to the container and issue a rebuild and continue command to the running dlv

**/*.go !**/*_test.go {
    prep +onchange: docker cp ./ app:/root/
    prep +onchange: timeout 1 dlv connect localhost:2345 --init dlv.txt
}

You'll be able to set breakpoints and everything, but you'll notice that sometimes you'll need to manually pause and continue to recover the debugging session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文