title: How to debug maven surefire tests running in a docker container?
body: <p>I have a maven project that contains integration tests and I use the surefire plugin. I can run the tests with </p>
  
  <pre><code>mvn clean package
  </code></pre>
  
  <p>and that works fine. I can also remote debug the integration tests using intellij. To do so I execute </p>
  
  <pre><code>mvn clean package -Dmaven.surefire.debug
  </code></pre>
  
  <p>and in Intellij I create a Run/Debug Configuration of type "Remote" setting Host to localhost and Port to 5005. As soon as the console prints </p>
  
  <pre><code>Listening for transport dt_socket at address: 5005
  </code></pre>
  
  <p>I start the remote debugging in intellij and everything works fine. </p>
  
  <p>The thing is I want to execute the integration tests inside a docker container and especially also debug them inside the container. I have created a docker image with java, maven and all the other stuff my application needs. In the Entrypoint I start my application and then I start the integration tests with </p>
  
  <pre><code>mvn clean package -Dmaven.surefire.debug
  </code></pre>
  
  <p>inside the docker container. I run the docker container with the option</p>
  
  <pre><code>-p 5005:5005
  </code></pre>
  
  <p>to open that port for remote debugging. When running the container, at some point I can see the console output </p>
  
  <pre><code>Listening for transport dt_socket at address: 5005
  </code></pre>
  
  <p>But when I start the remote debugger in intellij, it won't connect but instead gives me the following error message</p>
  
  <pre><code>Error running '...':
  Unable to open debugger port (localhost:5005): java.net.SocketException "Connection reset"
  </code></pre>
  
  <p>and if I try again to start the debugger i get the error message</p>
  
  <pre><code>Unable to open debugger port (localhost:5005): java.net.IOException "handshake failed - connection prematurally closed"
  </code></pre>
  
  <p>But the port should be open, because executing </p>
  
  <pre><code>nc -zv localhost 5005
  </code></pre>
  
  <p>in the host (netcat) results in </p>
  
  <pre><code>Connection to localhost 5005 port [tcp/*] succeeded!
  </code></pre>
  
  <p>But if execute the same netcat command inside the docker container it results in </p>
  
  <pre><code>localhost [...] 5005 (?) : Connection refused
  </code></pre>
  
  <p>Can anybody tell me what I am doing wrong? I don't insist on debugging with intellij, but being able to debug the tests inside the docker container somehow would be amazing.</p>