ThreeDotsLabs / wild-workouts-go-ddd-example

Go DDD example application. Complete project to show how to apply DDD, Clean Architecture, and CQRS by practical refactoring.
https://threedots.tech
MIT License
5.24k stars 479 forks source link

Delve debugging? #45

Open davidspiess opened 2 years ago

davidspiess commented 2 years ago

I was wondering how to debug the go code in development? Normaly i would use delve to set breakpoints in VSCode , but i'm not quite sure how to do this in a docker-compose setup. Any insights would be appreciated.

davidspiess commented 2 years ago

Here is how i solved it right now. I appreciate any input, how others solve this!

The delve debugger compiles and executes the application, exposes the http server under port 8080 and the debug server under 12345. These ports are mapped in the docker-compose.yaml file (in my case) to 30001 and 30002. The launch.json configuration attaches the debugger instance to the running container. On code change reflex restarts the debugger.

Dockerfile

FROM golang:1.18-alpine AS base
WORKDIR /app

FROM base AS development
RUN apk add build-base
# Create external debugger
RUN go install github.com/go-delve/delve/cmd/dlv@latest
# Restart server on code change
RUN go install github.com/cespare/reflex@latest
COPY reflex.conf /
ENTRYPOINT ["reflex", "-c", "/reflex.conf"]

reflex.conf

# Start delve debugger
-sr '(\.go$|go\.mod|\.tmpl$)' -- \
  dlv debug --headless --listen=:12345 --accept-multiclient --continue

docker-compose.yaml

 api:
    build:
      context: .
      target: development
    volumes:
      - .:/app
    ports:
      - 30001:8080
      - 30002:12345

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to container",
      "type": "go",
      "request": "attach",
      "mode": "remote",
      "debugAdapter": "dlv-dap",
      "port": 30002,
      "substitutePath": [{ "from": "${workspaceFolder}", "to": "/app" }]
    }
  ]
}