[Docker] Enable IntelliJ debug Java application with docker

Mostly package will deliver in container image, and for testing / debugging on dockerfile, we will run it locally. However, it cannot be debug easily. So we need to alter configuration to enable it.

In this demo, we will show how to enable debug in IntelliJ.

Prerequisites

  1. Applications below should be installed latest version and configuretd properly in local computer.

    1. IntelliJ;

    2. Docker;

  2. Plugin Docker should be installed in IntelliJ properly;

  3. Project related Dockerfile should be placed properly;

Steps

  1. Create Dockerfile debug configuration.
    In IntelliJ, select Run > Edit Configuration > click “+” and select Dockerfile under Item Docker.

  2. Configure Dockerfile configuration.
    In new dockerfile configuration, change properties as below.
    Dockerfile: dockerfile
    Context folder: build\libs (Jar file path)
    Command: java -jar <<jar path configurated in dockerfile>> (Example: java -jar /opt/test-service.jar)
    Bind port: 5050:5050 <<jar port to be used>>(Example: 5050:5050 8080:8080)

  3. Enable deploy built jar from current source code.
    In Before launch, click “+” and select Run Gradle task, in Gradle project select project, input build in Tasks. To speed-up build process, it can add -x test in Arguments.
    Then click OK to save settings.

  4. Add Remote debug configuration.
    In Run / Debug configuration, click “+” and select Remote JVM Debug.

  5. Configure remote debug settings.
    In Use module classpath, select project main class.

  6. Enable run docker before start debug.
    In Before launch menu, click “+”, select Launch docker before debug. select docker configuration which done in previous step and input -p 5005:5005 in custom options.
    then click OK to apply settings.

  7. Test debug can be run or not.
    Add break points in code, and select remote debug configure. Then run it in debug mode.
    After container connected and executed. Call related API to check breakpoint can be trigger or not.

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


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