我正在Docker上运行一个Go应用程序,并希望使用VSCode通过DLV调试它,同时使用模组进行应用程序重建。到目前为止,我还不知道如何连接到调试器。
码头工人:
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
模式:
**/*.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",
}
Q:如何利用MODD?使DLV在码头上工作
谢谢!
发布于 2022-04-16 01:53:03
好吧,afaik --做你想做的事情并不容易,因为你必须看着你的主机中的文件被更改,以触发容器内的dlv,从而扰乱vscode正在进行的调试会话。
下面是设置vscode在容器中调试应用程序的hacky方法,并在文件更改时使用modd重新启动容器内的调试。
(确保在主机上安装了modd )
..vscode/unch.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组合运行容器,并在后台运行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
}
您将能够设置断点和所有内容,但您会注意到,有时需要手动暂停并继续恢复调试会话。
https://stackoverflow.com/questions/71746173
复制相似问题