20

For some reason I have issues connecting remote debug to a spring-boot app running inside docker. I start the java app with:

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar app.jar

For docker I expose these ports on docker-compose:

ports:
- "8080:8080"
- "8000:8000"

However, the debugger is not able to connect on port 8000. It works when I run the server locally but not inside docker. Any idea why?

Docker ps output:

CONTAINER ID        IMAGE                       COMMAND                CREATED               STATUS              PORTS                                            NAMES
0d17e6851807        pocmanager_manager:latest   "/bin/sh -c 'java -D   3 seconds ago       Up 2 seconds        0.0.0.0:8000->8000/tcp, 0.0.0.0:8080->8080/tcp   pocmanager_manager_1   
35ed2e2c32bc        redis:latest                "/entrypoint.sh redi   14 seconds ago      Up 13 seconds       0.0.0.0:6379->6379/tcp                           pocmanager_redis_1
Jarle Hansen
  • 1,993
  • 2
  • 16
  • 29
  • What are you using as Host for the debugger. If you have only tried with localhost you must try with 127.0.0.1. This is,if your docker container is on the same machine running your eclise. If you have hosted your container on a VMBox then you might have to provide the IP of the host. – Johnson Abraham Jun 26 '15 at 10:49
  • I am using intellij, I try connecting to 192.168.59.103 port 8000. Its the ip of the boot2docker instance. – Jarle Hansen Jun 26 '15 at 10:51
  • Can you provide the output of "docker ps" – Johnson Abraham Jun 26 '15 at 10:58
  • Can you still access the app on 8080 with the same IP. Things look good to me atleast from your docker ps output. – Johnson Abraham Jun 26 '15 at 11:19
  • Yeah, the webapp works – Jarle Hansen Jun 26 '15 at 11:21
  • Hm, may be this article will help somehow? http://ptmccarthy.github.io/2014/07/24/remote-jmx-with-docker/ It describes the same problem, but with JMX enabled and the problem was that JMX has one port which dynamically allocated. – Slava Semushin Jun 26 '15 at 19:16
  • Thanks for the link, but I tried to set the RMI port as well and it did not work. I am not sure what I am doing wrong/missing here. – Jarle Hansen Jun 30 '15 at 10:24
  • For some reason it worked when I built the image manually (using the maven plugin) and then started it with docker compose. Previously I was also building the image with compose. Not sure why it did not work. – Jarle Hansen Jun 30 '15 at 11:00

5 Answers5

12

i have to realize that in the dockerFile the Expose command only do the half of work, this mean that only expose the port inside the docker, but not outside, in your example the result will be like this:

enter image description here

Debug works with the JAVA_OPTS and remote debug, the dockerFile looks like this:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar" ]

and executing this command:

docker run -e "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y" -p 8080:8080 -p 8000:8000 -t springio/gs-spring-boot-docker

As you can see, you should expose the debug port, during the run, in my case(eclipse) 8000

enter image description here

enter image description here

nekperu15739
  • 3,311
  • 2
  • 26
  • 25
10

Hi I faced the same problem.

I added the following to the entrypoint in the Dockerfile:

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

Now it looks like this:

FROM java:8
VOLUME /tmp
ADD realName*.jar app.jar
EXPOSE 4786
RUN sh -c 'touch /app.jar'
ENTRYPOINT
["java","-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

I did not Exposeport 8000 in the Dockerfile.

Hope this helps.

SWiggels
  • 2,159
  • 1
  • 21
  • 35
2

For me it was not enough to just specify the debug port within the address command line parameter. I also needed to wildcard all ip addresses by prefixing the port with *::

java -agentlib:jdwp=transport=dt_socket,address=*:8000,server=y,suspend=n -jar spring-app.jar
Marcus K.
  • 980
  • 11
  • 26
1

I had an issue connecting to a Spring Boot application running with Docker Compose and solved it with the settings below:

Project structure

- project
    - some-api
        - src
        - Dockerfile
        - build.gradle
    - docker-compose.yml        

project/docker-compose.yml

version: "3.9"
services:
  api:
    image: rbento/some-api
    build:
      context: ./some-api
      dockerfile: Dockerfile
    container_name: api
    ports:
      - 8080:8080
      - 5005:5005

project/some-api/Dockerfile

FROM adoptopenjdk/openjdk11:ubi
COPY build/libs/some-api-1.0.0.jar api.jar
ENTRYPOINT ["java","-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=n","-Djava.security.egd=file:/dev/./urandom","-jar","/api.jar"]

IntelliJ IDEA

Create a Remote JVM Debug configuration, like so:

  • Name: some-api-debug
  • Debugger mode: Attach to remote JVM
  • Host: localhost
  • Port: 5005
  • Command line arguments for remote JVM:
    -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
  • Use module classpath: some-api.main

Debugging

  • Run the application with docker-compose up
  • Then debug via IntelliJ IDEA

Output

Connected to the target VM, address: 'localhost:5005', transport: 'socket'

Environment

  • macOS Big Sur 11.5.2
  • IntelliJ IDEA CE 2021.2.1
  • Docker Version 3.6.0 (3.6.0.5487)
rbento
  • 9,919
  • 3
  • 61
  • 61
0

I think the reason for this might be, your Virtual Box VM configuration does not tunnel the debug port to the host machine.

Check this link https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

Basically, in your case, you have to command prompt and run

VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000";

Note : Make sure VBoxManage is in your PATH

Palanivelrajan
  • 121
  • 1
  • 10