Debugging an Application Running in Docker With IntelliJ IDEA – 用IntelliJ IDEA调试运行在Docker中的应用程序

最后修改: 2022年 8月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll see how to debug a Docker container in IntelliJ. We assume that we have a Docker image ready for testing. There are various ways to build Docker images.

在本教程中,我们将看到如何在IntelliJ中调试一个Docker容器。我们假设我们有一个Docker镜像可供测试。有多种方法可以构建Docker镜像

IntelliJ can be downloaded from its official website.

IntelliJ可以从其官方网站下载。

For this article, we’ll refer to this single class-based Java application. It can be easily dockerized, built, and tested.

在本文中,我们将提到这个基于单一类的Java应用程序。它可以很容易地dockerized、构建和测试

Before starting the testing, we need to make sure that the Docker engine is started and running on our computer.

在开始测试之前,我们需要确保Docker引擎已经在我们的电脑上启动和运行。

2. Using the Dockerfile Configuration

2.使用Dockerfile配置

When using the Docker file configuration, we need to simply select our Dockerfile and provide appropriate names for image name, image tag, container name, and configuration name. We may also add port mappings, if any:

当使用Docker文件配置时,我们需要简单地选择我们的Dockerfile,并为图像名称、图像标签、容器名称和配置名称提供适当的名称。我们还可以添加端口映射,如果有的话。

configuration using docker file

Once this configuration is saved, we can select this configuration from the debug option and hit debug. It first builds the image, registers the image in the Docker engine, and then runs the dockerized application.

一旦这个配置被保存下来,我们就可以从调试选项中选择这个配置并点击调试。它首先构建镜像,在Docker引擎中注册该镜像,然后运行docker化的应用程序。

3. Using the Docker Image Configuration

3.使用Docker镜像配置

When using the Docker image configuration, we need to provide the image name, image tag, and container name of our application that we’ve pre-built. We can use standard Docker commands to build the image and register the container in the Docker engine. We may also add port mappings, if any:

当使用Docker镜像配置时,我们需要提供我们预先构建的应用程序的镜像名称、镜像标签和容器名称。我们可以使用标准的Docker命令来构建镜像并在Docker引擎中注册容器。我们还可以添加端口映射,如果有的话。

configuration using docker image

Once this configuration is saved, we can select this configuration from the debug option and hit debug. It simply selects the pre-built Docker image and container and runs it.

一旦这个配置被保存下来,我们就可以从调试选项中选择这个配置并点击调试。它只是选择预建的Docker镜像和容器并运行它。

4. Using the Remote JVM Debug Configuration

4.使用远程JVM调试配置

A remote JVM configuration attaches itself to any pre-running Java process. So we need to run the Docker container individually first.

远程JVM配置会依附于任何预先运行的Java进程。所以我们需要先单独运行Docker容器。

Here’s the command to run the Docker image for Java 8:

下面是运行Java 8的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" docker-java-jar:latest

If we were using Java 11, we’d use this command instead:

如果我们使用的是Java 11,我们会用这个命令来代替。

docker run -d -p 8080:8080  -p 5005:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=n" docker-java-jar:latest

Here docker-java-jar is our image name, and latest is its tag. Apart from the normal HTTP port, which is 8080, we are also mapping an additional port, 5005, for remote debugging using the -p extension. We are using the -d extension for running docker in detached mode and -e for passing JAVA_TOOL_OPTIONS as an environment variable to the Java process.

这里docker-java-jar是我们的镜像名称,latest是其标签。除了正常的HTTP端口,也就是8080,我们还映射了一个额外的端口,5005用于使用-p扩展进行远程调试。我们使用-d扩展来运行docker的分离模式,-eJAVA_TOOL_OPTIONS作为一个环境变量传递给Java进程

In the JAVA_TOOL_OPTIONS we pass the value -agentlib:jdwp=transport=dt_shmem,address=,server=y,suspend=n to allow the Java process to start a JDB debug session and pass the value address=*:5005 to specify that 5005 will be our remote debugging port.

JAVA_TOOL_OPTIONS中,我们传入-agentlib:jdwp=transport=dt_shmem,address=,server=y,suspend=n,以允许Java进程启动JDB调试会话,传入address=*:5005以指定5005将是我们远程调试端口。

So the above command starts our Docker container, and we can now configure remote debugging configuration to connect to it:

因此,上述命令启动了我们的Docker容器,现在我们可以配置远程调试配置来连接到它。

configuration using remote jvm debug

We can see that in the configuration we’ve specified it to connect to the remote JVM using the 5005 port.

我们可以看到,在配置中我们已经指定它使用5005端口连接到远程JVM。

Now, if we select this configuration from the debug options and click debug, it will start a debug session by attaching to the already running Docker container.

现在,如果我们从调试选项中选择这个配置并点击调试,它将通过附加到已经运行的Docker容器上启动一个调试会话。

5. Conclusion

5.总结

In this article, we learned different configuration options we can use to debug a dockerized application in IntelliJ.

在这篇文章中,我们学习了不同的配置选项,可以用来在IntelliJ中调试一个docker化的应用程序。