【问题记录】idea连接docker中的tomcat容器debug

记录一次远程调试的问题

需求

idea debug remote docker tomcat

环境

docker image: tomcat:9.0.56-jdk17

错误提示

Error running Debugger: Unable to open debugger port (localhost:40003): 
java.io.IOException "handshake failed - connection prematurally closed"

错误原因

catalina.sh中自jdk9后,默认jpda使用loacalhost:8000作为socket监听地址,之前版本未对ip做限制。

Tomcat容器中catalina.sh部分命令

if [ "$1" = "jpda" ] ; then
  if [ -z "$JPDA_TRANSPORT" ]; then
    JPDA_TRANSPORT="dt_socket"
  fi
  if [ -z "$JPDA_ADDRESS" ]; then
    JPDA_ADDRESS="localhost:8000"
  fi
  if [ -z "$JPDA_SUSPEND" ]; then
    JPDA_SUSPEND="n"
  fi
  if [ -z "$JPDA_OPTS" ]; then
    JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
  fi
  CATALINA_OPTS="$JPDA_OPTS $CATALINA_OPTS"
  shift
fi

docker run命令

docker run --name=***** -tid \
 --restart=always \
 --link *****:***** \
 --link *****:***** \
 -p *****:8080 \
 -p 40003:8000 \
 -p *****:***** \
 -e ACCEPT_ORACLE_BCLA=***** \
 -v *****:/usr/local/tomcat/webapps \
 -v *****:/usr/local/tomcat/work \
 -v *****:/usr/local/tomcat/logs \
 -v *****:/etc/localtime:ro \
 -v *****:/etc/timezone:ro \
 $NAME:$VERSION \
 catalina.sh jpda run

docker ps -a

解决方案

docker run时设置环境变量覆盖默认的即可

-e JPDA_ADDRESS=*:8000

最终使用

【问题记录】idea连接docker中的tomcat容器debug_第1张图片

参考

https://stackoverflow.com/questions/30858312/handshake-failed-connection-prematurally-closed-error-when-debugging-solr-in-i

https://bugs.openjdk.java.net/browse/JDK-8175050

你可能感兴趣的:(问题记录,tomcat,intellij-idea,docker)