7

I developed a java program which is supposed to run in docker. However, I encountered a lot of pains when debugging my java program running in docker.

I searched on Internet, some tutorials proposed tools like spring-dev-tools (as my java program is a spring-boot-based program).

https://www.youtube.com/watch?v=sz5Zv5QQ5ek

Based on thoses tutorials, debugging is ok, such as setting breakpoint and variable watching, however, when I update my code (for instance, some classes), thoses changes cannot be reflected immediately in the program running in docker, the programm behaves as old code.

Can anybody give some hints ?

3
  • 4
    Is there any reason to debug / update the application while running in Docker? A better approach could be to have a local setup, easy to start and patch (like one started from IDE) until development is concluded. Commented Mar 24, 2019 at 10:00
  • 1
    The debugger must hotdeploy changed code Commented Mar 24, 2019 at 10:14
  • @nucandrei thanks for the reply. The reason is the program has some interactions with the host system, and also, I am using mac for dev, using local setup may not fit. It's a not a self-contained CRUD app in general, thus I have to put it in docker in the first place. Commented Mar 25, 2019 at 2:19

1 Answer 1

6

I have managed to make this work by doing the following:

  1. Mount the source code into the container at runtime
  2. Connect using a remote debugger

Here is my dockerfile:

FROM gradle:5.4-jdk12
WORKDIR /app
EXPOSE 8080 5005

and here is my docker-compose.yml:

version: '3'
services:
  app:
    build:
      context: .
    ports:
      - 5005:5005
      - 8080:8080
    volumes:
      - .:/app
    command: ["gradle", "bootRun"]

when I run this using docker-compose up and then connect a remote debugger, the hot reloading works.

I am connecting from Intellij so I have to rebuild the project for the hot reloading to work.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply, i managed to do this by firstly connecting to the 5005 port, and then click on 'Run' and then 'Reload changed Classes'. But this seems to be working with limited changes of code, if we, for instance, adding a class, this trick will not work.
OK, So, I initially had mounted only the source files and didn't see the changes reflected. But in this method, you are simply mounting the entire workspace. So whenever changes happen, IntelliJ compiles those classes and DevTools reloads. For hot reload to work, there should be a change in class files and not just the source file! Thanks btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.