java 在 docker 中调试 spring-boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31070671/
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-11-02 18:02:53  来源:igfitidea点击:

debug spring-boot in docker

javaspringdockerspring-boot

提问by Jarle Hansen

For some reason I have issues connecting remote debug to a spring-boot app running inside docker. I start the java app with:

出于某种原因,我在将远程调试连接到在 docker 中运行的 spring-boot 应用程序时遇到了问题。我使用以下命令启动 Java 应用程序:

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar app.jar

For docker I expose these ports on docker-compose:

对于 docker,我在 docker-compose 上公开这些端口:

ports:
- "8080:8080"
- "8000:8000"

However, the debugger is not able to connect on port 8000. It works when I run the server locally but not inside docker. Any idea why?

但是,调试器无法在端口 8000 上连接。当我在本地运行服务器但不在 docker 内部时,它可以工作。知道为什么吗?

Docker ps output:

Docker ps 输出:

CONTAINER ID        IMAGE                       COMMAND                CREATED               STATUS              PORTS                                            NAMES
0d17e6851807        pocmanager_manager:latest   "/bin/sh -c 'java -D   3 seconds ago       Up 2 seconds        0.0.0.0:8000->8000/tcp, 0.0.0.0:8080->8080/tcp   pocmanager_manager_1   
35ed2e2c32bc        redis:latest                "/entrypoint.sh redi   14 seconds ago      Up 13 seconds       0.0.0.0:6379->6379/tcp                           pocmanager_redis_1

回答by nekperu15739

i have to realize that in the dockerFile the Expose command only do the half of work, this mean that only expose the port inside the docker, but not outside, in your example the result will be like this:

我必须意识到在 dockerFile 中,Expose 命令只完成了一半的工作,这意味着只公开 docker 内部的端口,而不是外部,在您的示例中,结果将是这样的:

enter image description here

在此处输入图片说明

Debug works with the JAVA_OPTS and remote debug, the dockerFile looks like this:

调试与 JAVA_OPTS 和远程调试一起使用,dockerFile 如下所示:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar" ]

and executing this command:

并执行此命令:

docker run -e "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y" -p 8080:8080 -p 8000:8000 -t springio/gs-spring-boot-docker

As you can see, you should expose the debug port, during the run, in my case(eclipse) 8000

如您所见,您应该在运行期间公开调试端口,在我的情况下(eclipse)8000

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by SWiggels

Hi I faced the same problem.

嗨,我遇到了同样的问题。

I added the following to the entrypoint in the Dockerfile:

我在 Dockerfile 的入口点添加了以下内容:

"-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

“-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n”

Now it looks like this:

现在它看起来像这样:

FROM java:8
VOLUME /tmp
ADD realName*.jar app.jar
EXPOSE 4786
RUN sh -c 'touch /app.jar'
ENTRYPOINT
["java","-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

I did not Exposeport 8000 in the Dockerfile.

我没有Expose在 Dockerfile 中端口 8000。

Hope this helps.

希望这可以帮助。

回答by Palanivelrajan

I think the reason for this might be, your Virtual Box VM configuration does not tunnel the debug port to the host machine.

我认为原因可能是,您的 Virtual Box VM 配置没有将调试端口隧道连接到主机。

Check this link https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

检查此链接 https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

Basically, in your case, you have to command prompt and run

基本上,在您的情况下,您必须命令提示符并运行

VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000";

VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000";

Note : Make sure VBoxManage is in your PATH

注意:确保 VBoxManage 在您的 PATH 中