Skip to content

Commit b641d98

Browse files
author
Alexei Novikov
committedFeb 3, 2016
first commit
1 parent 8db5414 commit b641d98

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.class

‎Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM java:8
2+
MAINTAINER "Alexei Novikov <alexeinov@gmail.com>"
3+
COPY . /usr/src/myapp
4+
WORKDIR /usr/src/myapp
5+
RUN javac Hammer.java
6+
CMD ["java", \
7+
"-Dcom.sun.management.jmxremote", \
8+
"-Dcom.sun.management.jmxremote.port=9010", \
9+
# Uncomment this line when running JMX console on a different host
10+
#"-Dcom.sun.management.jmxremote.rmi.port=9010", \
11+
"-Dcom.sun.management.jmxremote.local.only=false", \
12+
"-Dcom.sun.management.jmxremote.authenticate=false", \
13+
"-Dcom.sun.management.jmxremote.ssl=false", \
14+
# Uncomment this line when running JMX console on a different host, use a name of the host where Docker is running
15+
#"-Djava.rmi.server.hostname=192.168.0.101", \
16+
"Hammer"]
17+
EXPOSE 9010

‎Hammer.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Hammer {
2+
public static void main(String[] args) {
3+
System.out.println("I'm a hammer. I'll be banging forever!");
4+
while (true) {
5+
try {
6+
System.out.print(".");
7+
Thread.sleep(3000);
8+
} catch (InterruptedException e) {
9+
break;
10+
}
11+
}
12+
}
13+
}

‎README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,55 @@
11
# 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+
```

‎UNLICENSE.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.