Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delve debugging? #45

Open
davidspiess opened this issue Nov 25, 2021 · 1 comment
Open

Delve debugging? #45

davidspiess opened this issue Nov 25, 2021 · 1 comment

Comments

@davidspiess
Copy link

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
Copy link
Author

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" }]
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant