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

When I launch the Docker container directly on my host, I can connect to it without any problems.

My host has a network of 192.168.1.0/24, and the host IP address is 192.168.1.20. My Docker container has an IP address of 172.17.0.2. When I connect to 172.17.0.2:1099 from jconsole, it works.

When I put this service in a Docker machine, I cannot connect to it.

My Docker machine has an IP of 192.168.99.100, and the container in it has an IP address of 172.17.0.2, but when I use jconsole to connect to 192.168.99.100:1099, it does not work.

To repeat this:

192.168.1.20 --- 172.17.0.2:1099 works

192.168.1.20 --- (192.168.99.100 --- 172.17.0.2:1099) and the connection to 192.168.99.100:1099 from my host does not work.

It is worth saying that I can access the container services in the Docker machine through the external IP address of the Docker device, for example. this will work:

192.168.99.100 --- (192.168.99.100:8080 --- 172.17.0.2:8080)

But when I use JMX, it just doesn't work.

This is a Tomcat service. I have this in scripts that run an instance of Tomcat:

CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n \ -Dcom.sun.management.jmxremote.port=1099 \ -Dcom.sun.management.jmxremote.rmi.port=1099 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false \ -Djava.rmi.server.hostname=IP address of Docker container 
+5
source share
2 answers

I think the problem is probably the value of the java.rmi.server.hostname property. This must be the host name or IP address that the JMX client must use to connect to your JVM. This is the first case when you directly connect to the container using 172.17.0.2:1099 , this parameter must be set to 172.17.0.2 . In the latter case, when you access the container through the docker machine at 192.168.99.100:1099 , the parameter must be set to 192.168.99.100 .

During my research for a very similar question (which was deleted in the meantime), I came across a blog post (which was also deleted). Although it's quite old, it gave me an idea of ​​how the JMX connection works:

  • The JMX registry is listening on the container <com.sun.management.jmxremote.port> port
  • If you connect to the registry using JConsole, the registry provides the client with the JMX service URL.
  • This URL is used by the client to receive JMX objects.

The service URL looks like this: service:jmx:rmi:///jndi/rmi://<java.rmi.server.hostname>:<com.sun.management.jmxremote.rmi.port>/jmxrmi . This is in your case service:jmx:rmi:///jndi/rmi://172.17.0.2:1099/jmxrmi . Since this address is available only on docker, connection to the remote control is not possible. In my question, I am addressing the same issue regarding the RMI port ...

There seems to be no ready-made solution to this problem. However, when starting the container, you can specify both the JMX port and the external host name (or IP) as environment variables, as suggested here . They can then be used in the JMX configuration:

 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 ...

+4
source

If anyone has a problem with this. I started the java process in a docker container with the following parameters:

 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9876 -Dcom.sun.management.jmxremote.rmi.port=9876 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=<name of the docker container> 

An important role is to set the name of the docker container. EXPOSE port in container 9876. I also established an ssh connection and forwarded 9876 to the local host.

The following are your SSH settings:

 LocalForward 127.0.0.1:9876 127.0.0.1:9876 

I also have setup / etc / hosts on the local machine

 127.0.0.1 <name of the docker container> 

Now connect the console to the "docker container name"

+1
source

Source: https://habr.com/ru/post/1243257/


All Articles