Giter VIP home page Giter VIP logo

go-container-debugging's Introduction

Debugging a Go executable in a Docker Container with VS Code

This repository illustrates how to debug into a Docker container running a Go binary.

Prerequisites:

In a nutshell

  1. Build the Go binary (for Linux) by running make
  2. Build the Docker container by running docker-compose up --build
  3. Open the code in Visual Studio by running code .
  4. Set some breakpoints in the code
  5. Start Debug into Docker in the Debug section of VS Code
  6. Open http://localhost:8080 in your browser
  7. See the breakpoints being hit
  8. Profit!

Details

Now, the application is a simply webserver that prints the current time on every page refresh. By default, it runs on http://localhost:8080.

The Dockerfile however won't start the executable. Instead, it will install the delve debugger and start it in headless mode. In addition to the application port 8080, it will open a debugger port at 2345.

When starting a VS Code debugging session, it will connect to that port 2345 and will make the debugger start the application. So now you can hit http://localhost:8080 and breakpoints should work as expected.

Notice that when you stop the debugger, the container will also be shut down and you might want to restart it.

References

[1] Remote debugging Go code with VS Code: https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code#remote-debugging

License

MIT.

go-container-debugging's People

Contributors

olivere avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-container-debugging's Issues

security_opt required for Linux

On my CentOS Linux machine, I have to uncomment these security options:

# These settings may be important, but I found them to NOT be required
# security_opt:
# - "seccomp:unconfined"

If not, I get this error:

▰ master go-container-debugging ▸ docker-compose up --build
Creating network "gocontainerdebugging_default" with the default driver
Building app
Step 1/6 : FROM golang
 ---> f50db16df5da
Step 2/6 : WORKDIR /go/src/github.com/olivere/go-container-debugging
 ---> Using cache
 ---> ac7270887d71
Step 3/6 : EXPOSE 8080 2345
 ---> Using cache
 ---> aad1efccc5d1
Step 4/6 : RUN go get github.com/derekparker/delve/cmd/dlv
 ---> Using cache
 ---> b41bda8c957b
Step 5/6 : ADD . /go/src/github.com/olivere/go-container-debugging
 ---> 3623b102b681
Step 6/6 : CMD ["dlv", "debug", "github.com/olivere/go-container-debugging", "--headless", "--listen=:2345", "--api-version=2", "--log"]
 ---> Running in ea53cba10718
Removing intermediate container ea53cba10718
 ---> b47bb192f0c6
Successfully built b47bb192f0c6
Successfully tagged gocontainerdebugging_app:latest
Creating gocontainerdebugging_app_1 ... 
Creating gocontainerdebugging_app_1 ... done
Attaching to gocontainerdebugging_app_1
app_1  | API server listening at: [::]:2345
app_1  | 2019-07-16T14:10:25Z info layer=debugger launching process with args: [/go/src/github.com/olivere/go-container-debugging/__debug_bin]
app_1  | could not launch process: fork/exec /go/src/github.com/olivere/go-container-debugging/__debug_bin: operation not permitted
gocontainerdebugging_app_1 exited with code 1

Apart from this, it works great. Thanks for the demo!

go-container-debugging_app_1 exited with code 1

Attaching to go-container-debugging_app_1 app_1 | API server listening at: [::]:2345 app_1 | 2020-03-14T19:09:38Z info layer=debugger launching process with args: [/go/src/github.com/olivere/go-container-debugging/__debug_bin] app_1 | could not launch process: fork/exec /go/src/github.com/olivere/go-container-debugging/__debug_bin: operation not permitted go-container-debugging_app_1 exited with code 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.