profile
viewpoint

Ask questionsUsing jmx in JAVA_OPTS make other java tools fail

I am trying to enable jmx on jenkins but when i do that i cannot use other java tools, like gradle, because they will inherit JAVA_OPTS.

Dockerfile that reproduces the problem:

FROM jenkins/jenkins:lts

USER root

RUN apt-get update && apt-get install -y gradle

ENV JAVA_OPTS "\
 -Dcom.sun.management.jmxremote.local.only=false\
 -Dcom.sun.management.jmxremote.port=8011\
 -Dcom.sun.management.jmxremote.rmi.port=8011\
 -Dcom.sun.management.jmxremote.authenticate=false\
 -Dcom.sun.management.jmxremote.ssl=false\
 -Dcom.sun.management.jmxremote.ssl.need.client.auth=false\
 -Dcom.sun.management.jmxremote.registry.ssl=false\
 -Dcom.sun.management.jmxremote.authenticate=false\
 -Dcom.sun.management.jmxremote=true\
 -Dcom.sun.management.jmxremote.host=0.0.0.0\
"

Command:

docker build . -t jenkins-with-gradle
docker exec -it $(docker run -d -p 8080:8080 -p 50000:50000 jenkins-with-gradle) gradle`

Output:

Error: JMX connector server communication error: service:jmx:rmi://0.0.0.0:8011
sun.management.AgentConfigurationError: java.rmi.server.ExportException: Port already in use: 8011; nested exception is:
	java.net.BindException: Address already in use (Bind failed)
	at sun.management.jmxremote.ConnectorBootstrap.exportMBeanServer(ConnectorBootstrap.java:800)
	at sun.management.jmxremote.ConnectorBootstrap.startRemoteConnectorServer(ConnectorBootstrap.java:468)
	at sun.management.Agent.startAgent(Agent.java:262)
	at sun.management.Agent.startAgent(Agent.java:452)
Caused by: java.rmi.server.ExportException: Port already in use: 8011; nested exception is:
	java.net.BindException: Address already in use (Bind failed)
	at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:346)
	at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:254)
	at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
	at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
	at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:236)
	at sun.management.jmxremote.ConnectorBootstrap$PermanentExporter.exportObject(ConnectorBootstrap.java:199)
	at javax.management.remote.rmi.RMIJRMPServerImpl.export(RMIJRMPServerImpl.java:146)
	at javax.management.remote.rmi.RMIJRMPServerImpl.export(RMIJRMPServerImpl.java:122)
	at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:404)
	at sun.management.jmxremote.ConnectorBootstrap.exportMBeanServer(ConnectorBootstrap.java:796)
	... 3 more
Caused by: java.net.BindException: Address already in use (Bind failed)
	at java.net.PlainSocketImpl.socketBind(Native Method)
	at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
	at java.net.ServerSocket.bind(ServerSocket.java:375)
	at java.net.ServerSocket.<init>(ServerSocket.java:237)
	at sun.management.jmxremote.ConnectorBootstrap$HostAwareSocketFactory.createServerSocket(ConnectorBootstrap.java:864)
	at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:666)
	at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:335)
	... 12 more

As a workaroud, i changed jenkins.sh to also read java options from JENKINS_JMX_OPTS:

  while IFS= read -r -d '' item; do
      java_opts_array+=( "$item" )
    fi
  done < <([[ $JENKINS_JMX_OPTS ]] && xargs printf '%s\0' <<<"$JENKINS_JMX_OPTS")

Or maybe just unsetting JAVA_OPTS before this line would solve the problem.

jenkinsci/docker

Answer questions IuryAlves

We have a seed job on our Jenkins and the it has to run on Jenkins controller (Formerly known as master).

useful!
source:https://uonfu.com/
answerer
iury IuryAlves @voiapp Stockholm, Sweden https://medium.com/@iurysouza Always tinkering, sometimes writing code
Github User Rank List