- Install Docker Integration Plugin to IntelliJ as explained here https://www.jetbrains.com/help/idea/docker.html
- Restart IntelliJ
ScreenShot: Docker IntelliJ Settings
- Go to Settings/Preferences dialog, click Docker under Build, Execution, Deployment.
- Add a Docker configuration (The Add (+) button) and specify how to connect to the Docker daemon. I was running on Mac, and selected the
Docker for Mac option
- I left the Path mappings with default /Users option
- The connection settings depend on your Docker version and operating system. For more information, check
Enable Docker Support
section here: https://www.jetbrains.com/help/idea/docker.html - If it is set correct, you will see a
Connection Successful
message at the bottom of the dialog.
ScreenShot: Docker Tool Window
- Go to your project, and open the
Docker Tool Window
from menuView -> Tool Windows -> Docker
- You can see Containers and Images in your local Docker in this tool window. You can also Start/Stop Docker Plugin from this tool window.
- You can create/start/Stop/Delete Containers and Images from here.
ScreenShot: Dockerfile Build Config
I dynamically build Dockerfile with build.sbt. So, added this to build.sbt as shown in the screenshot above. So, Update your build file:
- Make sure you add this to existing/new JAVA_OPTS ENV variable:
-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y
- Also expose debug port 5005
expose(5005)
I am showing example with SBT build here. So, sbt clean docker
will create a docker image and shows it in IntelliJ Docker Tool Window. Click on the image in Docker Tool Window and Note the Image tag name to use while setting up run config next. (Note the text version, as it will remain same for every build)
ScreenShot: Docker Image Run Config
- Add a New Run Configuration (Run -> Edit Configuration -> Add(+); Select
Docker
). - Add Image tage notes above to the
Image ID
field in the dialog. - Give a static Container Name in the dialog (Ex: app-debug).
- Bind any app exposed ports to the host. Also a port that will be used by the debugger. Ex: In my akka application, I have 2559 port exposed. So, I added two 2559:2559 and 5005:5005
- if your app needs any environment variables add that to the dialog.
- you can preview your docker command for this run configuration from
Command Preview
- Save the Configuration (Apply/OK)
ScreenShot: Remote Debugging Run Config
- Add a New Run Configuration (Run -> Edit Configuration -> Add(+); Select
Remote
). - It pre-populates command line args. But of you want to change the debug port from default 5005, you can change that where
Port:
field is. - Select your project for the module's classpath.
- Save the Configuration (Apply/OK)
- Put some breakpoints in the code from IntelliJ
- Run the Docker Run Configuration created above in Step 6. It will start and wait with message "
Listening for transport dt_socket at address: 5005
- Now Run the Remote Debugging configuration created above in Step 7. It will start and show this on console
Connected to the target VM, address: 'localhost:5005', transport: 'socket'
- Now the application continues through its startup (can see on Docker Tool Window logs).
- Testing application to hit debug breakpoints should hit them, and from there on you will have all debugging features like Step Into, Step Over, Step Out, Resume etc.
- https://www.jetbrains.com/help/idea/docker.html
- https://blog.jetbrains.com/idea/2017/11/what-does-intellij-idea-2017-3-have-in-store-for-docker-support/
- https://docs.oracle.com/javase/6/docs/technotes/guides/jpda/architecture.html
- https://docs.oracle.com/javase/6/docs/technotes/guides/jpda/jpda.html#walk-through
- https://stackoverflow.com/questions/3591497/java-remote-debugging-how-does-it-work-technically
- https://www.youtube.com/watch?v=sz5Zv5QQ5ek