github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/docs/contributing/debug.md (about)

     1  ### Debugging the daemon
     2  
     3  The Docker daemon inside the development container can be debugged with [Delve](https://github.com/go-delve/delve).
     4  
     5  Delve debugger listens on a port, which has to be exposed outside the development container.
     6  Also, in order to be able to debug the daemon, it has to be compiled with the debugging symbols.
     7  This can be done by launching the development container with the following command:
     8  
     9  ```bash
    10  $ make BIND_DIR=. DOCKER_DEBUG=1 DELVE_PORT=127.0.0.1:2345:2345 shell
    11  ```
    12  
    13  The `DOCKER_DEBUG` variable disables build optimizations, allowing to debug the binary,
    14  while `DELVE_PORT` publishes the specified port for use with the debugger.
    15  
    16  The `DELVE_PORT` variable accepts the port in the same format as Docker CLI's `--publish` (`-p`) option.
    17  This means that the port can be published in multiple ways:
    18  
    19  1. `DELVE_PORT=127.0.0.1:2345:2345` - exposes debugger on port `2345` for local development only (recommended)
    20  2. `DELVE_PORT=2345:2345` - exposes debugger on port `2345` without binding to specific IP
    21  3. `DELVE_PORT=2345` - same as above
    22  
    23  **IMPORTANT:** Publishing the port without binding it to localhost (127.0.0.1) might expose the debugger
    24  outside the developer's machine and is not recommended.
    25  
    26  ## Running Docker daemon with debugger attached
    27  
    28  1. Run development container with build optimizations disabled (ie. `DOCKER_DEBUG=1`) and Delve enabled:
    29     ```bash
    30     $ make BIND_DIR=. DOCKER_DEBUG=1 DELVE_PORT=127.0.0.1:2345:2345 shell
    31     ```
    32  2. Inside the development container:
    33     1. Build the Docker daemon:
    34        ```bash
    35        $ ./hack/make.sh binary
    36        ```
    37     2. Install the newly-built daemon:
    38        ```bash
    39        $ make install
    40        ```
    41     3. Run the daemon through the `make.sh` script:
    42        ```bash
    43        $ ./hack/make.sh run
    44        ```
    45        The execution will stop and wait for the IDE or Delve CLI to attach
    46        to the port, specified with the `DELVE_PORT` variable.
    47        Once the IDE or Delve CLI is attached, the execution will continue.
    48  
    49  ## Running integration tests with debugger attached
    50  
    51  1. Run development container with build optimizations disabled (ie. `DOCKER_DEBUG=1`) and Delve enabled:
    52  
    53     ```bash
    54     $ make BIND_DIR=. DOCKER_DEBUG=1 DELVE_PORT=127.0.0.1:2345:2345 shell
    55     ```
    56  
    57  2. Inside the development container, run the integration test you want through the `make.sh` script:
    58  
    59     ```bash
    60     $ TEST_INTEGRATION_DIR=./integration/networking \
    61         TESTFLAGS='-test.run TestBridgeICC' \
    62         ./hack/make.sh dynbinary test-integration
    63     ```
    64  
    65     The execution will pause and wait for the IDE or Delve CLI to attach
    66     to the port, specified with the `DELVE_PORT` variable.
    67     Once the IDE or Delve CLI is attached, the test execution will start.
    68  
    69  ## Debugging from IDE (on example of GoLand 2021.3)
    70  
    71  1. Open the project in GoLand
    72  2. Click *Add Configuration* on the taskbar
    73     ![GoLand - adding configuration](images/goland_add_config.png)
    74  3. Create the *Go Remote* configuration. 
    75     No changes are necessary, unless a different port is to be used.
    76     ![GoLand - adding remote configuration](images/goland_debug_config.png)
    77  4. Run the Docker binary in the development container, as described in the previous section.
    78     Make sure that the port in the `DELVE_PORT` variable corresponds to one, used in the *Go Remote* configuration.
    79  5. Run the *Go Remote* configuration.
    80     The Docker daemon will continue execution inside the container and debugger will stop it on the breakpoints.
    81     ![GoLand - run Go Remote configuration](images/goland_run_debug_config.png)
    82  
    83  ## Where to go next
    84  
    85  Congratulations, you have experienced how to use Delve to debug the Docker daemon
    86  and how to configure an IDE to make use of it.