Home > Software design >  How to Remote debug of java application running as docker container in Intellij
How to Remote debug of java application running as docker container in Intellij

Time:02-21

In the below example since I have many environment variables I cannot directly run ENTRYPOINT with java -jar command option. Instead I have used entrypoint.sh

Dockerfile

ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,address=9251,server=y,suspend=n
RUN ["chmod", "777", "entrypoint.sh"]
ENTRYPOINT ["bash","entrypoint.sh"]
EXPOSE 9251

entrypoint.sh

java $JVM_OPTS $JAVA_OPTS org.springframework.boot.loader.JarLauncher

docker-compose.yaml

version: '3.1'

services:
  bus:                                           
    image: sample-image
    network_mode: "host"
    ports:
      - 9251:9251
    environment:
      - DEBUG_PORT=9251
      - JVM_OPTS=-Xms1G -Xmx5G -XX:MaxMetaspaceSize=512m -XX: UseG1GC

In the below snapshot we can see when the service is started with debugging enabled enter image description here

when running sudo netstat -tulpn | grep :9251 shows

The docker is running in a linux box which I am trying to connect from local windows machine. Also note that it is mandatory that I need to keep the network_mode as host.

enter image description here

enter image description here

CodePudding user response:

Docker part:

Can you try to remove a host network mode? And check whether it works.

Alternatively I don't see a reason to use port forwarding here, these two configurations are kind of mutually exclusive.

From the documentation:

If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.

And then:

Note: Given that the container does not have its own IP-address when using host mode networking, port-mapping does not take effect, and the -p, --publish, -P, and --publish-all option are ignored

So bottom line you should not deal with port forwarding in the network-mode=host and just make sure that your port is available on the host machine.

Java part:

It looks like your "java" part is ok, however it has a nuance: depending on the actual java version the remote server options slightly change:

For example in java 11 you should use:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9251

And in java 8:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=:9251

Note the "*" that has changed. So you should try that...

  • Related