如何将VisualVM附加到在Docker容器中运行的简单Java进程 [英] How do I attach VisualVM to a simple Java process running in a Docker container
问题描述
实际上我想要一个适用于JEE容器的解决方案,特别是Glassfish,但在我尝试了很多设置组合并且没有成功之后,我将设置简化为最简单的情况。
Actually I wanted a solution working for JEE containers, specifically for Glassfish, but after I tried many combinations of settings and did not succeed, I reduced the setup to the simplest possible case.
这是我在Docker容器中启动的Hello World守护进程。我想将 jconsole
或 VisulaVM
附加到它。一切都在同一台机器上。
Here is my Hello World daemon started in a Docker container. I want to attach jconsole
or VisulaVM
to it. Everything is on the same machine.
public class Main {
public static void main(String[] args) {
while (true) {
try {
Thread.sleep(3000);
System.out.println("Hello, World");
} catch (InterruptedException e) {
break;
}
}
}
}
Dockerfile
Dockerfile
FROM java:8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN javac Main.java
CMD ["java", "Main"]
构建: docker build -t hello-world-daemon。
正在运行: docker run -it --rm --name hwd hello-world-daemon
问题:
- 应将哪些JVM参数添加到
CMD
命令行? - 应该公开和发布哪些端口?
- Docker容器应该使用什么网络模式?
- what JVM parameters should be added to
CMD
command line? - what ports should be exposed and published?
- what network mode should Docker container be using?
我没有在这里显示我失败的尝试,因此正确答案不会有偏见。这应该是一个非常常见的问题,但我找不到可行的解决方案。
I do not show my failed attempts here so that correct answers will not be biased. This should be a pretty common problem, yet I could not find a working solution.
更新。工作解决方案
此Dockerfile正常工作
This Dockerfile works
FROM java:8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN javac Main.java
CMD ["java", \
"-Dcom.sun.management.jmxremote", \
"-Dcom.sun.management.jmxremote.port=9010", \
"-Dcom.sun.management.jmxremote.local.only=false", \
"-Dcom.sun.management.jmxremote.authenticate=false", \
"-Dcom.sun.management.jmxremote.ssl=false", "Main"]
EXPOSE 9010
结合docker run命令
in combination with the docker run command
docker run -it --rm --name hwd -p 9010:9010 hello-world-daemon
VisualVM
通过右键单击 Local-> Add JMX Connection 进行连接,然后输入 localhost:9010
,或通过添加远程主机。
VisualVM
connects via right click Local->Add JMX Connection, and then entering localhost:9010
, or through adding a remote host.
JConsole
通过选择连接a 远程进程, localhost:9010
。
JConsole
connects via selecting a Remote process with localhost:9010
.
将连接定义为远程时,可以使用 ifconfig
列出的任何接口。例如, docker0
接口,地址 172.17.0.1
有效。容器的地址 172.17.0.2
也可以。
When defining the connection as remote, any interface listed by ifconfig
can be used. For instance, docker0
interface with address 172.17.0.1
works. The container's address 172.17.0.2
works too.
推荐答案
首先你应该用这些JVM参数运行你的应用程序:
At first you should run you application with these JVM params:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
然后你应该公开docker的端口:
Then you should expose port for docker:
EXPOSE 9010
使用docker run命令指定端口绑定:
Also specify port binding with docker run command:
docker run -p 9010:9010 -it --rm --name hwd hello-world-daemon
之后,您可以将Jconsole连接到本地9010端口并管理在Docker中运行的应用程序。
After that you can connect with Jconsole to local 9010 port and manage application run in Docker.
这篇关于如何将VisualVM附加到在Docker容器中运行的简单Java进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!