Search code examples
dockerosgiapache-karafkaraf

How to configure jmx port for karaf in docker container?


I would like to use visualvm to check threads running in the karaf which is in a docker container.

What I did:

  1. expose 44444 and 1099 for the docker
  2. in org.apache.karaf.management.cfg, tried setting rmiRegistryHost and rmiServerHost several times with different combination of docker container ip and docker inner ip which doesn't work.
  3. tried change EXTRA_JAVA_OPTS="-Djava.rmi.server.hostname=${docker-container-ip} -Dcom.sun.management.jmxremote.local.only=false"

Need help.


Solution

  • The problem is the RMI protocol, that does not handle the scenario well where the host offering the RMI endpoint (the Docker host) is not the the host of the RMI server (the VM inside your Docker container).

    The way I got it to work was to

    export EXTRA_JAVA_OPTS=="-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.rmi.port=$JMX_RMI_PORT -Dcom.sun.management.jmxremote.port=$JMX_REMOTE_PORT -Djava.rmi.server.hostname=$HOST_HOSTNAME"
    

    I set the environment with docker-compose, but you can replace the environment variables with fixed values as long as you run only one container with the image on your host. I am using 1097 and 1098 for the RMI and REMOTE ports respectively. Two things are important here:

    • Both the RMI and REMOTE ports have to be mapped to your docker host on the same port number. E.g. if you use 1097 as the RMI port in your container, then you have to publish that port to 1097 on the host.
    • The java.rmi.server.hostname is the hostname or IP of your docker host, not the IP of the container

    Assuming you are using the ports above, then connecting to

    service:jmx:rmi:///jndi/rmi://<your_docker_host>:1098/jmxrmi
    

    should now work.

    I never got Karaf's JMX accecss control to work though.

    Using JMXMP instead of RMI for JMX might make things easier, but it is not supported by Karaf out of the box.