• Dockerfile
FROM golang:1.23-alpine

WORKDIR /app

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

COPY go.mod go.sum ./
RUN go mod download

CMD ["air", "-c", ".air.toml"]

 

  • docker-compose
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 2345:2345
    volumes:
      - ./:/app
  • .air.toml
...
[build]
  args_bin = []
  # bin = ";APP_ENV=development ./tmp/main"
  full_bin = "APP_ENV=development dlv exec ./tmp/main --headless=true --listen=:2345 --api-version=2 --accept-multiclient"
...

 

  • .vscode/launch.json
{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Debug Golang",
			"type": "go",
			"request": "attach",
			"mode": "remote",
			"remotePath": "",
			"port":2345,
			"host":"localhost",
			"showLog": true,
			"trace": "verbose",
			"logOutput": "rpc"
		}
	]
}

 

Chạy docker compose up , chạy debug trong vscode và gắn breakpoint.