Run and debug a Spring Boot application using Docker Compose
Last modified: 10 August 2022Required plugins: Docker, Spring, and Spring Boot (bundled)
You can use IntelliJ IDEA to run and debug a Spring Boot application running in multiple Docker containers under Docker Compose. This tutorial describes how to run two Docker Compose services inside containers: a simple Spring Boot application and a MySQL database. The application can receive GET requests that add entries to the database. This tutorial also describes how you can set breakpoints and debug your application using a remote debug configuration.
Before you begin:
Install and run Docker.
For more information, see the Docker documentation.
Configure the Docker daemon connection in IntelliJ IDEA.
For more information, see Enable Docker support.
The source code of the application is hosted on GitHub at https://github.com/IdeaUJetBrains/SpringBootDockerDemoDebug
From the main menu, select VCS | Get from Version Control
Specify the URL of the repository and click Clone.
If necessary, agree to open the cloned project in a new window.
![Clone sample Spring Boot project for Docker Clone sample Spring Boot project for Docker](https://resources.jetbrains.com/help/img/idea/2022.1/docker_spring_boot_clone.png)
Let's see how the application works before running it in debug mode.
Open the docker-compose-debug.yml file.
Click
in the gutter next to
services
.
This creates a Docker Compose run configuration, which starts the application in a container as the app
service and the database in another container as the db
service. If successful, you should see something similar to the following in the build log:
⠿ Container db Healthy 10.6s
⠿ Container springbootdockerdemodebug-app-1 Started 10.9s
![Running Spring Boot application in Services tool window Running Spring Boot application in Services tool window](https://resources.jetbrains.com/help/img/idea/2022.1/docker_spring_boot_running_services.png)
You can access the application at http://localhost:18080. For example, you can try to execute the following GET request, which should add a new entitybus
entry to the database and list all available entries: http://localhost:18080/entitybus/post.
![Execute a GET request Execute a GET request](https://resources.jetbrains.com/help/img/idea/2022.1/docker_spring_boot_get_request.png)
You can connect to the database at jdbc:mysql:entitybus
table. Use the following connection settings:
Host:
0.0.0.0
orlocalhost
or127.0.0.1
Port:
13306
User:
root
Password:
root
Database:
DOCKERDB
![Connect to MySQL Connect to MySQL](https://resources.jetbrains.com/help/img/idea/2022.1/docker_spring_boot_mysql_connection.png)
To debug the application, you need a remote debug configuration that will first run the application in Docker Compose with a custom command for debugging, and then attach to the debugger.
Open the docker-compose-debug.yml file.
Click
in the gutter next to
command
under theapp
service.Select the module in the Use module classpath list.
Double-click the Docker Compose run configuration in the Before launch list. If it is not in the list, click
and select Launch Docker before debug.
Select the Docker Compose run configuration with the app service. Also check the Custom Command field: it should contain the
-agentlib
option and options from thecommand
field in the docker-compose-debug.yml file:java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 -Djava.security.egd=file:/dev/./urandom -jar /project/target/demo-0.0.1-SNAPSHOT.jar
note
If the default port is allocated to something else, change the container port for the Java debugger to connect to and make sure that the port in the remote debug configuration is the same.
The previous command example works for JDK 9 and later versions. If you are using JDK 8 or earlier version, specify the address as
address=5005
.
![Configure remote JVM debug for Docker Compose Configure remote JVM debug for Docker Compose](https://resources.jetbrains.com/help/img/idea/2022.1/docker_compose_spring_debug.png)
If the application is already running from the previous step, apply the changes and do not run the debug configuration for now.
If the application is already running, stop it. To do it, select the Docker Compose node in the Services tool window and click
in the toolbar. Alternatively, you can right-click and delete the containers under the corresponding services.
Open the docker-compose-debug.yml file.
Click
in the gutter and start the debug configuration.
Once the application starts and the debugger attaches to it, the Debug tool window will open.
![The Debug tool window The Debug tool window](https://resources.jetbrains.com/help/img/idea/2022.1/docker_spring_boot_debug_tool_window.png)
Open the src
/main file and set a breakpoint in the/java /entity /Entitybus.java setEid()
method.Execute the following GET request: http://localhost:18080/entitybus/post.
The application will stop at the breakpoint and you can examine the current variable values and frames in the Debug tool window.
Click
until it executes the request and you get the returned values.
Thanks for your feedback!