Docketコンテナ内で動くGoのサーバーにデバッガを設定する

Dockerコンテナで動くGoアプリをDelveを使ってデバッグできるようにするのが色々と苦戦したのでメモ。
Delveを使う。

Dockerfile

1
2
3
4
5
6
7
FROM golang:1.19

WORKDIR /app

RUN go install github.com/go-delve/delve/cmd/dlv@latest

CMD ["dlv", "debug", "./main.go", "--listen=:2345", "--headless", "--api-version=2", "--continue", "--accept-multiclient"]

Delve

--api-version=2

--continue

--accept-multiclient

Docker Compomse

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
version: '3'
services:
  golang-docker:
    init: true
    build:
      context: ./
      dockerfile: ./Dockerfile
    security_opt:
      - seccomp:unconfined
    volumes:
      - server-a-go-mod:/go/pkg/mod
      - server-a-go-build:/root/.cache/go-build
    ports:
      - '2345:2345'

security_opt seccomp:undefined

launch.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Connect to server",
      "type": "go",
      "request": "attach",
      "mode": "remote",
      "port": 2345,
      "host": "127.0.0.1",
      "substitutePath": [
        {
          "from": "${workspaceFolder}",
          "to": "/app"
        },
        {
          "from": "${env:GOPATH}",
          "to": "/go"
        },
        {
          "from": "${env:GOROOT}",
          "to": "/usr/local/go"
        }
      ]
    }
}
> back to posts