Boot

Spring Boot Docker Image Debugging

In this article, we will explain how to debug the spring boot application running in the docker image. We will see a sample spring boot application and debug it in the docker container.

1. Create a spring boot application

Create a simple spring-boot application using any IDE(in my case, I am using IntelliJ IDEA) of your choice with basic configuration and dependency. I am using gradle for the dependencies. You can use maven as well. Find the build.gradle file as shown below

build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.jcg'
version = '1.0'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

It has a simple controller with a simple greeting message as shown below:

DockerController

package com.jcg.sampledocker;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DockerController {

    @RequestMapping("/")
    public String greetings(@RequestParam String name) {
        String message =  " Hello " +name.toUpperCase() + ", from JCG";
        return message;
    }
}

Run the following command to build the application jar file.

./gradlew clean build

This will compile, build the application and generate the executable jar file in the build/libs folder. This jar file is used in the next steps

2. Create a docker file

Create the docker file and add the following line as shown below

Dockerfile

FROM openjdk:9-jre
ADD build/libs/sample-docker-1.0.jar .
EXPOSE 8080 5005
CMD java -jar sample-docker-1.0.jar

Here we are using the base image as openjdk:9-jre to build our container. It will copy the jar file into the docker image so that it’s available within the container. We are exposing two of the ports, as shown above. One is the default one (8080), and the other is for debugging (5005). Finally, we have the command to execute once the docker container runs.

3. Create a docker-compose file

Now create a docker compose file to setup the application. Add the following code into the docker-compose file. It basically specifies the ports and agent for remote debug as shown below

docker-compose.yml

services:
  web:
    build: .
    ports:
      - "8080:8080"
      - "5005:5005"
    command: java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar sample-docker-1.0.jar

Here in the command section we configure the JVM to allow debugging. We enable the agent Java Debug Wire Protocol (JDWP) inside the JVM. And the for the debug connections is set as 5005.

4. Running the application

4.1 Setup the remote debug

Inorder to debug the application first attach the remote debugger. You can do this in any IDE in the run configurations section. Please find the screenshot below

Boot Docker image - Attaching remote
Attaching remote debug

Here as shown in the image above we are using address as 5005 to listen to debug connections

4.2 Running the application

We will run the application using the docker-compose as shown below

docker-compose up

Once you run this you can see the server is listening to the debug connection in the port 5005 as shown

Boot Docker image - Server listening
Server listening at the configured port 5005

5. Debugging the application

Now once the server has started running, go and click the remote debugger which you have set in the step 4.1. And then add a breakpoint in the controller and enter the url localhost:8080?name=abc into the browser. You will see the control stopping at the breakpoint as shown below.

Boot Docker image - Debugging at the breakpoint
Debugging at the breakpoint

6. Summary

In this article we discussed about the debugging spring boot application in the docker image. We saw a sample application and configured the same for the debugging in the container.

You can find more Spring boot tutorials here.

7. Download the source code

This was an article of how to debug the spring boot application running in the docker image.

Download
You can download the full source code of this example here: Spring Boot Docker Image Debugging

Shankar Mata

Shankar works as a Lead Software Engineer in a leading company and has developed several applications. He is one of the decision makers for the design and architecture for all the applications in his team. He along with his team develop and deploy cloud native services. He had been working in the field of Java and cloud technologies. He is also Amazon and Microsoft certified solution architect for AWS and Azure respectively. He has done his Masters in Computer science from University of New Mexico, US and Bachelors from GITAM University India. He is also experienced in Angular Framework and developed several applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button