Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote debugging Java 9 in a docker container from IntelliJ IDEA

I have a Dockerfile with this content:

FROM openjdk:9

WORKDIR /project

ADD . /project

EXPOSE 5005

My docker-compose.yml looks like this:

version: "3.2"
services:
  some-project:
    build: .
    ports:
      - target: 5005
        published: 5005
        protocol: tcp
        mode: host
  command: "java '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005' SomeClass"

When I do docker-composer up I see a message "Listening for transport dt_socket at address: 5005". But when I try to connect with jdb or Idea I get "java.io.IOException: handshake failed - connection prematurally closed".

Everything works fine if I change openjdk:9 to openjdk:8. However, I need Java 9 for my project.

like image 553
Iurii Drozdov Avatar asked Oct 10 '17 08:10

Iurii Drozdov


People also ask

How do I debug a Docker container in IntelliJ?

Create a remote debug configuration From the main menu, select Run | Edit Configurations. and select Remote JVM Debug. and select Launch Docker Before Debug. Make sure that the ports in the Configure Docker dialog and in the remote debug configuration match.

Can you debug a Docker container?

The Docker extension currently supports debugging Node. js, Python, and . NET applications within Docker containers.

Can you remote into a Docker container?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.

Can I run IntelliJ in Docker container?

IntelliJ IDEA provides Docker support using the Docker plugin. The plugin is bundled and enabled by defaultin IntelliJ IDEA Ultimate Edition. For IntelliJ IDEA Community Edition, you need to install the Docker plugin as described in Install plugins.


1 Answers

From Java 9, the JDWP socket connector accept only local connections by default. See: http://www.oracle.com/technetwork/java/javase/9-notes-3745703.html#JDK-8041435

So, to enable debug connections from outside, specify *:<port> as address:

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

like image 85
Jorrit Posthuma Avatar answered Oct 20 '22 13:10

Jorrit Posthuma