|
1 | 1 | # docker-jmx-demo
|
2 |
| -A simple demo getting JMX metrics from a docker container |
| 2 | +## A simple demo getting JMX metrics from a docker container |
| 3 | + |
| 4 | +This demo is a Docker container that starts a simple Java application with pre-configured JMX options. Manipulating the options and running a container and JMX console in different modes demonstrates different cases of JMX monitoring. In all variants, JMX is bound to the port `9010`, so `JConsole` will always be connecting a remote host `hostname:9010`, even when running locally. |
| 5 | + |
| 6 | +### Build an image |
| 7 | + |
| 8 | +Run the `docker build` command in the project's directory. |
| 9 | +```bash |
| 10 | +docker build -t banging-daemon . |
| 11 | +``` |
| 12 | + |
| 13 | +### Bridge networking mode |
| 14 | + |
| 15 | +Bridge mode is the default for a Docker container. In this mode only the published container's ports are accessible |
| 16 | + |
| 17 | +#### Local `JConsole` |
| 18 | + |
| 19 | +However, when running `JConsole` on the same machine, RMI options do not need to be advertised. The following lines of the `Dockerfile` may stay disabled. |
| 20 | + |
| 21 | +```dockerfile |
| 22 | +#"-Dcom.sun.management.jmxremote.rmi.port=9010", \ |
| 23 | +#"-Djava.rmi.server.hostname=192.168.0.101", \ |
| 24 | +``` |
| 25 | +Run the container publishing the `JMX` remote port `9010`. |
| 26 | + |
| 27 | +```bash |
| 28 | +docker run -it --rm --name hammer -p 9010:9010 banging-daemon |
| 29 | +``` |
| 30 | + |
| 31 | +#### Remote `JConsole` |
| 32 | + |
| 33 | +When `JConsole` is running on a different machine, RMI options have to be set, as a randomly RMI port is not accessible by the remote machine. It is convenient to set the port `com.sun.management.jmxremote.rmi.port` to the same value as `com.sun.management.jmxremote.port`, which is `9010` in this configuration. Using same port number makes one port mapping configuration less in a Docker container and eventually in a firewall. Parameter `java.rmi.server.hostname` should point at a host where Docker is running. |
| 34 | + |
| 35 | +```dockerfile |
| 36 | +"-Dcom.sun.management.jmxremote.rmi.port=9010", \ |
| 37 | +"-Djava.rmi.server.hostname=192.168.0.101", \ |
| 38 | +``` |
| 39 | + |
| 40 | +### Host networking mode |
| 41 | + |
| 42 | +In the host mode, the container's network is not encapsulated at all. It is accessible alongside the host's network. In this case the RMI options do not need to be configured. |
| 43 | + |
| 44 | +```dockerfile |
| 45 | +#"-Dcom.sun.management.jmxremote.rmi.port=9010", \ |
| 46 | +#"-Djava.rmi.server.hostname=192.168.0.101", \ |
| 47 | +``` |
| 48 | + |
| 49 | +Running `JConsole` locally or remotely does not make any difference. |
| 50 | + |
| 51 | + The `JMX` remote port does not need to be published: |
| 52 | + |
| 53 | +```bash |
| 54 | +docker run -it --rm --net=host --name hammer banging-daemon |
| 55 | +``` |
0 commit comments