在 Docker Compose 中调试 gradle

问题描述 投票:0回答:1

我正在尝试调试在 docker 中运行的 gradle 应用程序,但是当我尝试在端口 5005 上连接调试器时,出现以下错误:

demo % jdb -attach localhost:5005                                                                                             

java.net.SocketException: Connection reset
        at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:318)
        at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:346)
        at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:796)
        at java.base/java.net.Socket$SocketInputStream.read(Socket.java:1099)
        at jdk.jdi/com.sun.tools.jdi.SocketTransportService.handshake(SocketTransportService.java:133)
        at jdk.jdi/com.sun.tools.jdi.SocketTransportService.attach(SocketTransportService.java:273)
        at jdk.jdi/com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:119)
        at jdk.jdi/com.sun.tools.jdi.SocketAttachingConnector.attach(SocketAttachingConnector.java:83)
        at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:563)
        at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:369)
        at jdk.jdi/com.sun.tools.example.debug.tty.Env.init(Env.java:63)
        at jdk.jdi/com.sun.tools.example.debug.tty.TTY.main(TTY.java:1176)

Fatal error:
Unable to attach to target VM.

我相信我正确地将端口 5005 映射到本地主机上的同一端口,并且应用程序记录它正在等待调试器连接,所以我不确定问题是什么。

我的 Dockerfile:

FROM gradle:8.1.1-jdk17 AS base
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src

FROM base AS development
EXPOSE 5005
CMD ["./gradlew", "bootRun", "--debug-jvm"]

FROM base AS build
RUN gradle build --no-daemon

FROM eclipse-temurin:17-jre-jammy AS production
EXPOSE 8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar /app/
ENTRYPOINT ["java", "-jar","/app/demo-0.0.1-SNAPSHOT.jar"]

我的撰写文件:

version: '3.8'
services:
  demo:
    build:
      context: .
      target: development
    ports:
      - "5005:5005"
      - "8080:8080"
    environment:
      - SERVER_PORT=8080
    volumes:
      - ./:/app

我的码头工人日志:

2024-01-06 17:56:51 demo-1  | Starting a Gradle Daemon, 4 busy and 1 stopped Daemons could not be reused, use --status for details
2024-01-06 17:56:54 demo-1  | > Task :checkKotlinGradlePluginConfigurationErrors
2024-01-06 17:56:56 demo-1  | > Task :processResources
2024-01-06 17:56:59 demo-1  | > Task :compileKotlin
2024-01-06 17:56:59 demo-1  | > Task :compileJava NO-SOURCE
2024-01-06 17:56:59 demo-1  | > Task :classes
2024-01-06 17:56:59 demo-1  | > Task :resolveMainClassName
2024-01-06 17:57:01 demo-1  | 
2024-01-06 17:57:01 demo-1  | > Task :bootRun
2024-01-06 17:57:01 demo-1  | Listening for transport dt_socket at address: 5005
docker gradle docker-compose
1个回答
0
投票

通过删除 Dockerfile 中的 --debug-jvm 参数并将以下内容添加到 build.gradle 中来对此进行排序:

bootRun {
    jvmArgs '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=*:5005'
}
© www.soinside.com 2019 - 2024. All rights reserved.