如何在 Intellij 中远程调试 java 应用程序 运行 作为 docker 容器
How to Remote debug of java application running as docker container in Intellij
在下面的示例中,因为我有很多环境变量,所以我不能直接使用 运行 ENTRYPOINT 和 java -jar 命令选项。相反,我使用了 entrypoint.sh
Docker 文件
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
在下面的快照中,我们可以看到服务何时在启用调试的情况下启动
当 运行ning sudo netstat -tulpn | grep :9251
显示时
docker 运行 在 linux 盒子里,我试图从本地 windows 机器连接。另请注意,我必须将 network_mode 保留为 host
.
Docker部分:
你能尝试移除一个host
网络模式吗?并检查它是否有效。
或者我看不出有什么理由在这里使用端口转发,这两种配置有点相互排斥。
来自文档:
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.
然后:
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
所以最重要的是你不应该在 network-mode=host 中处理端口转发,只需确保你的端口在主机上可用。
Java部分:
看起来你的“java”部分没问题,但它有细微差别:根据实际的 java 版本,远程服务器选项略有变化:
例如在 java 11 你应该使用:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9251
并且在 java 8:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=:9251
注意已更改的“*”。所以你应该试试...
在下面的示例中,因为我有很多环境变量,所以我不能直接使用 运行 ENTRYPOINT 和 java -jar 命令选项。相反,我使用了 entrypoint.sh
Docker 文件
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
在下面的快照中,我们可以看到服务何时在启用调试的情况下启动
当 运行ning sudo netstat -tulpn | grep :9251
显示时
docker 运行 在 linux 盒子里,我试图从本地 windows 机器连接。另请注意,我必须将 network_mode 保留为 host
.
Docker部分:
你能尝试移除一个host
网络模式吗?并检查它是否有效。
或者我看不出有什么理由在这里使用端口转发,这两种配置有点相互排斥。
来自文档:
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.
然后:
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
所以最重要的是你不应该在 network-mode=host 中处理端口转发,只需确保你的端口在主机上可用。
Java部分:
看起来你的“java”部分没问题,但它有细微差别:根据实际的 java 版本,远程服务器选项略有变化:
例如在 java 11 你应该使用:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9251
并且在 java 8:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=:9251
注意已更改的“*”。所以你应该试试...