Developing Spring Boot Application using Docker - Part 2

Hopefully you have now got your Spring Boot Application run inside the container. If you are not there yet, please visit Developing Spring Boot Application using Docker - Part 1 to go start.

There is one thing as an Architect on my daily job, I used to ask myself, if we run the application inside the container, how do we debug? How to view logs? How to triage issues on local?

The biggest of all is debugging the Spring Boot Application that is running inside the container.

Setting up Debugging environment

The concept of debugging any Java Based Application that is running inside the container can be done using the most common way is using remote debugging. Typically, whenever we wanted to debug applications that are running in a machine that is outside of our own, we use Remote Debugging. Most IDEs like Eclipse and IntelliJ ship with those capabilities.

The good news is, the setup required to get that started are fairly common. Lets see how to get that setup when running on docker.

To keep the running configuration from the debugging configuration, let us create new Dockerfile and a docker-compose.yml file.


docker-compose-debug.yml file

Now you have your application setup to be run in DEBUG mode. To start the application in debug mode, we need to run the below command.

$ docker-compose -f docker-compose-debug.yml up -d --build

This will start our application in debugging mode. Now that we have that started in debugging mode, it is time to integrate it with our IDEs.

For now I will go over IntelliJ IDEA and Eclipse as once you have this figured out in these IDEs, other IDEs will follow a similar technique.

Setting up IntelliJ for Debugging

Open the Spring Boot project inside IntelliJ and open the Edit Configurations... that is located near the Run/Debug gears.

Now, select the Remote JVM Debug.

We could potentially leave the configuration as is if the port is configured as 5005. If you cannot use port 5005 for some other reason, we need to choose the appropriate port select here and update our Dockerfile_debug and docker-compose-debug.yml file to use your port.

Thats it! Open the Controller and on the file gutter of the editor enable the break point. Start the Remote JVM Debug configuration.

Once you have enabled the debugger point test the endpoint. This will open the debugger at the break point.

Setting up Eclipse for Debugging

Make sure to open the Spring Boot Application inside Eclipse. Click on the Debug Configurations... on the IDE.

Now add a new Configuration by selecting Remote Java Application.

Please ensure to match the configuration as shown below.

Key Configuration Items:
  • Project: Select your Spring Boot Project
  • Connection Type: Standard (Socket Attached)
  • Connection Properties:
    • Host: localhost
    • Port: 5005
Hit Apply and Start Debugging if you have started your Docker in debug mode.

Add a break point on your code and test it. Eclipse will request for you to switch to Debug Perspective and it that all you have to do to debug your Spring Boot Applications using Eclipse.

Shutting down Debugging Docker

Please use the below command to bring down the running docker containers.

$ docker-compose -f docker-compose-debug.yml down

Next Steps

Now that we are able to run the Spring Boot Application inside a Docker container, it is time for us to add additional layers like adding a database connection and building our images.

The ultimate problem Docker solves is 
It works on my computer
 Now, since we have dockerized the Spring Boot Development, we can think about moving our applications into Docker.

The code for this part can be found here:
If you like the blog, poke me on my Twitter @reflexdemon


Popular posts from this blog

Java SSL/TLS Testing Tool: Cipher Suite

PWA: Building and publishing a PWA in 15ish minutes using Angular 5 and Service Worker

Yarn and Angular-CLI