How to connect with JMX from host to Docker container in Docker machine?

前端 未结 2 1594
醉酒成梦
醉酒成梦 2021-01-18 04:23

When I have running Docker container directly at my host, it is possible to connect to it without any problems.

My host has network 192.168.1.0/24 and IP address of

2条回答
  •  梦毁少年i
    2021-01-18 04:51

    I think the problem is probably the value of the java.rmi.server.hostname property. This needs to be the hostname or IP address that should be used by the JMX client to connect to your JVM. That is in the first case where you connect to your container directly using 172.17.0.2:1099, this setting needs to be set to 172.17.0.2. In the latter case where you access the container through the docker machine on 192.168.99.100:1099, the setting needs to be set to 192.168.99.100.

    During my research for a very similar question (which was deleted in the meantime) I stumbled across a blog entry (which was deleted in the meantime as well). Although It's rather old it gave me an idea how the JMX connectivity works:

    1. The JMX registry listens on port of the container
    2. If you connect to the registry with JConsole, the registry provides the JMX service URL to the client.
    3. This URL is used by the client to obtain the JMX objects

    The service URL looks like this service:jmx:rmi:///jndi/rmi://:/jmxrmi. That is in your case service:jmx:rmi:///jndi/rmi://172.17.0.2:1099/jmxrmi. As this address is only reachable from within the docker machine, connecting from remote is not possible. In my question I cover the same problem in regards to the RMI port...

    There doesn't seem to be an out-of-the-box solution to this problem. However one can provide both JMX port and the external hostname (or IP) on startup of the container as environment variables, as suggested here. These could then be used in the JMX config:

    docker run -p 1099:1099 \
        -e "JMX_HOST=192.168.99.100" \
        -e "JMX_PORT=1099" \
        company/tomcat:8.0.30
    

    and

    CATALINA_OPTS="... \
        -Dcom.sun.management.jmxremote=true \
        -Dcom.sun.management.jmxremote.port=$JMX_PORT \
        -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT \
        -Dcom.sun.management.jmxremote.authenticate=false \
        -Dcom.sun.management.jmxremote.ssl=false \
        -Djava.rmi.server.hostname=$JMX_HOST"
    

    Not very nice, but it should work...

提交回复
热议问题