I have a java application that is running inside a docker container. I have deployed this container in an ECS cluster. I want to expose a JMX port so I can collect JVM stats using CollectD agent installed on that machine.
The JVM params that I have specified in my Java app are
JAVA_OPTS="-Dspring.config.location=classpath:/base/ -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.net.preferIPv4Stack=true -Djava.rmi.server.hostname=10.0.7.118
I am able to connect to this JMX port if I run this application in a non docker environment. However, I am unable to do the same in Docker.
I have also given port mappings in My Task Definition so this port can be exposed to the outer world. I know, If I was running this docker using docker run command than I could have specified -p param for port mapping, but I can not do the same here as I am running this app on an ECS cluster which deploys this image. So I have to rely on port mappings provided by task definition.
TaskDefnition
"ContainerDefinitions": [
{
"Name": "MyApplication",
"Cpu": "2048",
"Essential": "true",
"Image": "location of the image",
"Memory": "8192",
"MemoryReservation": "4096",
"Environment": [
{
"Name": "Test",
"Value": {
"Fn::GetAtt": [
"SomeAttrib",
"SomeAccessKey"
]
}
}
],
"PortMappings": [
{
"HostPort": "8080",
"ContainerPort": "8080"
},
{
"HostPort": "8008",
"ContainerPort": "8008"
}
]
}
After going through various links I found the solution to my problem. Finally, JVM params look like this
Adding -Dcom.sun.management.jmxremote.local.only=true did the trick for me. You can set it to true or false depending on your requirement.