Apache Cassandra in Docker with JMX enabled

Apache Cassandra in Docker with JMX enabled

It's a bit tricky to enable JMX for Cassandra when running in Docker. I create a Cassandra Docker image alexcheng/cassandra of version 3.11.2 with JMX authentication enabled, see the Dockerfile. The username and password to access JMX are both cassandra. The environment variable JMX_HOST has the default value of localhost, and it should work most of the time. If localhost doesn't work , you can use the host's IP address. After starting the container, you can access JMX using VisualVM or JConsole with address service:jmx:rmi://localhost/jndi/rmi://localhost:7199/jmxrmi. Change localhost to the same value as JMX_HOST if localhost is not used. Since SSL is not enabled, you need to use insecure connections.

On Mac, the local IP address can be obtained using ipconfig getifaddr en0, so we can use following command to start the container when using IP address is required.

docker run --rm -p 7199:7199 -e "JMX_HOST=`ipconfig getifaddr en0`" alexcheng/cassandra

Below are some details about JMX in Docker container.

To enable JMX connection in the container, we need to set following system variables.

  • com.sun.management.jmxremote
  • com.sun.management.jmxremote.port
  • com.sun.management.jmxremote.rmi.port
  • java.rmi.server.hostname

When SSL is not enabled, com.sun.management.jmxremote.port and com.sun.management.jmxremote.rmi.port can set to the same value. The port needs to be exported in the Docker image. java.rmi.server.hostname needs to be set for VisualVM and JConsole to connect to the JMX server, otherwise you may encounter the error java.lang.SecurityException: Expecting a sun.rmi.server.UnicastRef2 remote reference in stub.

To enable JMX authentication, following system variables are required.

  • com.sun.management.jmxremote.authenticate=true
  • com.sun.management.jmxremote.password.file
  • com.sun.management.jmxremote.access.file

The password file has the username/password for users, while the access file has the permissions for each user.

More details about these two files can be found here.

© 2023 VividCode