How do I attach VisualVM to a simple Java process running in a Docker container

后端 未结 6 1768
礼貌的吻别
礼貌的吻别 2020-12-12 14:35

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

6条回答
  •  不思量自难忘°
    2020-12-12 14:59

    FWIW, this is how I was able to attach VisualVM to a Java process inside a Docker container running on macOS:

    Main.java:

    public class Main {
        public static void main(String args[]) throws Exception {
            while (true) {
                System.out.print("Hello ");
                System.out.println("world");
                Thread.sleep(1000);
            }
        }
    }
    

    Dockerfile:

    FROM openjdk:11.0.2-slim
    COPY Main.class /
    WORKDIR /
    ENTRYPOINT ["java", \
    "-Dcom.sun.management.jmxremote=true", \
    "-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", \
    "-Dcom.sun.management.jmxremote.rmi.port=9010", \
    "-Djava.rmi.server.hostname=localhost", \
    "Main"]
    

    Compile the Java code, build the image and run the container like this:

    $ javac Main.java
    $ docker build -t main .
    $ docker run -p 9010:9010 -it main
    

    Then attach VisualVM using JMX to localhost:9010

提交回复
热议问题