最近想學習下java GC優化,就用了一下VisualVM,在遠端伺服器啟動了一個非docker的tomcat,很順利的就連線了,但是用docker-compose啟動的服務卻
怎麼也連不上,一定是docker的鍋.
最終找到了解決方法,在這裡 https://forums.docker.com/t/enable-jmx-rmi-access-to-a-docker-container/625
這篇文章中最重要的一句話就是
In my case, I am working with docker-compose please DON FORGET to expose the SAME PORT 6001 in the remote docker host (it won't work if you expose in another port differently that jmxremote.port and jmxremote.rmi.port):
一句話概括就是: docker-compose暴露的埠要跟jmx的埠一樣
好了,問題解決,下面寫出tomcat配置jmx的方法:
1. docker-compose.yml
web: image: "registry.xxxxxx.com/msgbox/base_tomcat:tomcat7.42_jdk8u40" ports: - 9911:8080 - 9912:8787 - 44445:44445 volumes: - ./confs:/data1/confs - ./web/webapps:/data1/xxx/webapps - ./authconfs:/data1/authconfs - ./web/logs:/data1/xxx/logs environment: - NAME_CONF=test-yf=/data1/xxx/bin/./ - RUN_COMMAND=catalina.sh jpda run - JMX_PORT=44445 - JMX_HOST=10.77.6.164 - CATALINA_HEAP=-server -Xmx768m -Xmn100m -Xms768m links: - kafka
2. setenv.sh
if [[ ! -z "$JMX_PORT" ]]; then if [ -z "$JMX_HOST" ]; then JMX_HOST=$(hostname -i) fi CATALINA_EXTRA="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=${JMX_PORT} -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access -Djava.rmi.server.hostname=${JMX_HOST} -Dcom.sun.management.jmxremote.rmi.port=${JMX_PORT}" fi
完成