Sometimes it's challenging to track down the cause of unexpected behavior in an app. Because ko makes it simple to make tweaks to your app and immediately rebuild your image, it's possible to iteratively explore various aspects of your app, such as by adding log lines that print variable values.
But to help you solve the problem as fast as possible, ko supports debugging your Go app with delve.
To use this feature, just add the --debug flag to your ko build command. This adjusts how the image is built:
- It installs
delvein the image (in addition to your own app). - It sets the image's
ENTRYPOINTto adelve exec ...command that runs the Go app in debug-mode, listening on port40000for a debugger client. - It ensures your compiled Go app includes debug symbols needed to enable debugging.
Note: This feature is geared toward development workflows. It should not be used in production.
Build the image using the debug feature.
ko build . --debug
Run the container, ensuring that the debug port (40000) is exposed to allow clients to connect to it.
docker run -p 40000:40000 <img>
This sets up your app to be waiting to run the command you've specified. All that's needed now is to connect your debugger client to the running container!
By default, the application will not start until a debugger has connected and issued the continue command. This is required in order to be able to set breakpoints for code that is executed during start-up. If you want the application to start running without waiting for a debugger, use the --debug-continue parameter:
ko build . --debug --debug-continue