Java 如何使用 IntelliJ 调试在 Docker 中运行的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34157900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:22:03  来源:igfitidea点击:

How to debug an application running in Docker with IntelliJ?

javaintellij-ideadockerremote-debugging

提问by Somaiah Kumbera

I have a Jetty application running in docker. I would like to debug this application using my local IntelliJ. I am on v 14.1, so I have installed the Docker Integration plugin.

我有一个在 docker 中运行的 Jetty 应用程序。我想使用本地 IntelliJ 调试此应用程序。我使用的是 v 14.1,所以我安装了 Docker 集成插件。

Under Clouds, I am using the default values that showed up when I click on the '+'. IntelliJ docs say this should be OK. Here the

在 Clouds 下,我使用的是单击“+”时显示的默认值。IntelliJ 文档说这应该没问题。这里的

API URL: http://127.0.0.1:2376
Certificates folder: <empty>

I'm not sure what these are used for, so I dont know if these values are right.

我不确定这些是做什么用的,所以我不知道这些值是否正确。

Under Run/Debug configurations, I am using Docker Deployment, and the following values:

在运行/调试配置下,我正在使用 Docker 部署和以下值:

Deployment: Docker Image
Image ID: The docker image ID 
Container name: The name of the container

When I try to run this, I get javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: Connect to http://127.0.0.1:2376[/127.0.0.1] failed: Connection refused

当我尝试运行它时,我得到 javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: Connect to http://127.0.0.1:2376[/127.0.0.1] failed: Connection denied

Obviously the API URL value I am using is incorrect. Any suggestions on what that value should be?

显然,我使用的 API URL 值不正确。关于该值应该是什么的任何建议?

My debugging options are:

我的调试选项是:

 -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n -Djava.compiler=NONE

采纳答案by Somaiah Kumbera

Sheesh Never mind. I didnt really need the Docker Integration plugin. Seems like that is more for deployment and management of Docker directly through Intellij than for debugging.

谢什别介意。我真的不需要 Docker 集成插件。看起来这更像是直接通过 Intellij 部署和管理 Docker,而不是调试。

To debug my jetty app running inside my docker container, I simply remote debugged:

为了调试在我的 docker 容器内运行的码头应用程序,我只是远程调试:

Run | Edit configurations | + | Remote

运行 | 编辑配置 | + | 偏僻的

The command line args were already OK since I used the default remote debugging options. I only needed to change the Host settings. Here I used the hostname I had set within the docker container

因为我使用了默认的远程调试选项,所以命令行参数已经可以了。我只需要更改主机设置。这里我使用了我在 docker 容器中设置的主机名

回答by Bojan Vukasovic

If anybody wants to do development on windows machine, and run/debug app at same time on a remote docker, you can check my intellij plugin here: https://bojanv55.wordpress.com/2018/08/03/intellij-idea-remote-debug-of-java-code-inside-docker-container/

如果有人想在 Windows 机器上进行开发,并同时在远程 docker 上运行/调试应用程序,您可以在这里查看我的 intellij 插件:https://bojanv55.wordpress.com/2018/08/03/intellij-idea -remote-debug-of-java-code-inside-docker-container/

回答by Nathan Niesen

In Java 8 the JDK supports a JAVA_TOOL_OPTIONS environment variable so to enable the debugger for any Java application you can add the following parameters to your docker runcommand:

在 Java 8 中,JDK 支持 JAVA_TOOL_OPTIONS 环境变量,因此要为任何 Java 应用程序启用调试器,您可以将以下参数添加到您的docker run命令中:

-p 8000:8000 -e "JAVA_TOOL_OPTIONS=\"-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n\""

Then start a remote debug session connecting to localhost:8000.

然后启动连接到 localhost:8000 的远程调试会话。

回答by Rachangouda Patil

Run the docker image like below:

运行 docker 镜像,如下所示:

docker run -d -p 8080:8080  -p 5005:5005 \
    -e JAVA_TOOL_OPTIONS="agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" \
    imagename:tagname

Intellij configuration Steps:

Intellij配置步骤:

1) From the menu bar click on runEdit Configurations→ in the left panel click on Remote→ click on +symbol to add the debug config

1) 从菜单栏点击runEdit Configurations→ 在左侧面板点击Remote→ 点击+符号添加调试配置

2) After adding a new config, debug mode=Attachto remote JVM. Fill the host and port number

2)添加新配置后,调试mode=Attach到远程JVM。填写主机和端口号

3) Select the module classpath to debug then apply the settings

3) 选择要调试的模块类路径,然后应用设置

4) To connect run the above remote config from the runmenu.

4) 要连接,请从run菜单运行上述远程配置。

example config

示例配置