Why can't I debug my Go project using docker in VS Code?

66 Views Asked by At

I'm starting a project in Go, which I will work using docker. These are my files:

Dockerfile:

FROM golang:1.22

WORKDIR /go/src/gitlab.com/main/server

COPY go.mod .
COPY go.sum .

RUN go mod download

RUN go install github.com/cosmtrek/air@latest
RUN go install github.com/swaggo/swag/cmd/swag@latest
RUN go install github.com/go-delve/delve/cmd/dlv@latest

EXPOSE 2345
EXPOSE 80

Docker compose:

version: "3.2"
services:
  service-influxdb:
    image: go-time-series
    build:
      context: ..
      dockerfile: docker/Dockerfile
    command: air -c .air.toml
    volumes:
      - ../:/go/src/gitlab.com/main/server
    ports:
      - 8080:80
      - 10000:2345
    environment:
      - WEB_APP_VERSION=0.0.1
      - WEP_APP_TITLE=Thori influx-DB
      - WEP_APP_DESCRIPTION=Micro service for the manage of the influx-db services
      - ENVIRONMENT=dev
      - HOST=localhost:8080
      - INFLUX_URL=http://localhost:8086
      - INFLUX_TOKEN=###############
      - INFLUX_ORG=123acs
      - INFLUX_MINIMUN_DATE=1677-09-21 00:12:44
      - INFLUX_MAXIMUN_DATE=2100-01-01 00:00:00

.air.toml:

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "./tmp/main"
  full_bin = "dlv exec --accept-multiclient --headless --listen :2345 --api-version 2 ./tmp/main"
  pre_cmd = ["swag init -g ./cmd/main.go --cf multi"]
  cmd = "go build -o ./tmp/main ./cmd/main.go"
  post_cmd = []
  delay = 0
  exclude_dir = ["assets", "tmp", "vendor", "testdata", "docs"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = true
  follow_symlink = true
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  poll = false
  poll_interval = 0
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  time = true

[misc]
  clean_on_exit = true

[screen]
  clear_on_rebuild = true
  keep_scroll = true

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug in Docker",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/go/src/gitlab.com/main/server",
            "port": 10000,
            "host": "127.0.0.1",
            "showLog": true,
            "trace": "log",
            "logOutput": "rpc"
        }
    ]
}

func main() {
    port := flag.Int("port", 80, "Listening port")
    flag.Parse()

    if config.Cfg.Environment == "prod" {
        gin.ForceConsoleColor()
        gin.SetMode(gin.ReleaseMode)
    }

    router := gin.Default()
    loadSwagger(router)
    loadAPI(router)
    router.Run(fmt.Sprintf(":%d", *port))
}

But when I raise my container and start the debug it does not recognize the breakpoints

enter image description here

What could be happening? Why can't it find the file? (the path is correct) How to solve it? Thank you

1

There are 1 best solutions below

0
Diego L On

I solved it by adding substitutePath instead of remotePath in the launch.json:

        {
            "name": "Debug in Docker",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            // "remotePath": "/go/src/gitlab.com/main/server",
            "substitutePath": [
                { "from": "${workspaceFolder}", "to": "/go/src/gitlab.com/main/server"},
            ],
            "port": 10000,
            "host": "127.0.0.1",
            "showLog": true,
            "trace": "log",
            "logOutput": "rpc"
        }

https://github.com/golang/vscode-go/wiki/debugging#remote-debugging