Signed-off-by: Albin Kerouanton <albinker@gmail.com>
3.6 KiB
Debugging the daemon
The Docker daemon inside the development container can be debugged with Delve.
Delve debugger listens on a port, which has to be exposed outside the development container. Also, in order to be able to debug the daemon, it has to be compiled with the debugging symbols. This can be done by launching the development container with the following command:
$ make BIND_DIR=. DOCKER_DEBUG=1 DELVE_PORT=127.0.0.1:2345:2345 shell
The DOCKER_DEBUG
variable disables build optimizations, allowing to debug the binary,
while DELVE_PORT
publishes the specified port for use with the debugger.
The DELVE_PORT
variable accepts the port in the same format as Docker CLI's --publish
(-p
) option.
This means that the port can be published in multiple ways:
DELVE_PORT=127.0.0.1:2345:2345
- exposes debugger on port2345
for local development only (recommended)DELVE_PORT=2345:2345
- exposes debugger on port2345
without binding to specific IPDELVE_PORT=2345
- same as above
IMPORTANT: Publishing the port without binding it to localhost (127.0.0.1) might expose the debugger outside the developer's machine and is not recommended.
Running Docker daemon with debugger attached
- Run development container with build optimizations disabled (ie.
DOCKER_DEBUG=1
) and Delve enabled:$ make BIND_DIR=. DOCKER_DEBUG=1 DELVE_PORT=127.0.0.1:2345:2345 shell
- Inside the development container:
- Build the Docker daemon:
$ ./hack/make.sh binary
- Install the newly-built daemon:
$ make install
- Run the daemon through the
make.sh
script:
The execution will stop and wait for the IDE or Delve CLI to attach to the port, specified with the$ ./hack/make.sh run
DELVE_PORT
variable. Once the IDE or Delve CLI is attached, the execution will continue.
- Build the Docker daemon:
Running integration tests with debugger attached
-
Run development container with build optimizations disabled (ie.
DOCKER_DEBUG=1
) and Delve enabled:$ make BIND_DIR=. DOCKER_DEBUG=1 DELVE_PORT=127.0.0.1:2345:2345 shell
-
Inside the development container, run the integration test you want through the
make.sh
script:$ TEST_INTEGRATION_DIR=./integration/networking \ TESTFLAGS='-test.run TestBridgeICC' \ ./hack/make.sh dynbinary test-integration
The execution will pause and wait for the IDE or Delve CLI to attach to the port, specified with the
DELVE_PORT
variable. Once the IDE or Delve CLI is attached, the test execution will start.
Debugging from IDE (on example of GoLand 2021.3)
- Open the project in GoLand
- Click Add Configuration on the taskbar
- Create the Go Remote configuration. No changes are necessary, unless a different port is to be used.
- Run the Docker binary in the development container, as described in the previous section.
Make sure that the port in the
DELVE_PORT
variable corresponds to one, used in the Go Remote configuration. - Run the Go Remote configuration. The Docker daemon will continue execution inside the container and debugger will stop it on the breakpoints.
Where to go next
Congratulations, you have experienced how to use Delve to debug the Docker daemon and how to configure an IDE to make use of it.