Recording a Dockerized Application
As Docker is a "closed environment", it can be challenging to create a JFR recording of an application running in a Docker or Kubernetes environment. You can use different approaches, depending on the connections you have available and where you can store the recording. When you are using the jcmd
tool, or record at startup, you can store the recording inside the container, or use a volume to make the file easier accessible.
Recording with jcmd via Docker
Use the jcmd
tool via the command-line tool via docker exec
to start and stop JFR recordings.
-
Find the process ID of the Java application in the Docker container by opening a shell connection and executing the
jcmd
command:$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b94b5825a39f petclinic:1.0 "java -jar petclinic…" 57 seconds ago Up 56 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp agitated_cerf $ docker exec -it b94b5825a39f sh # jcmd 1 petclinic.jar 82 jdk.jcmd/sun.tools.jcmd.JCmd -
Start a JFR Recording in the Docker container, see JFR Command Line Options for a complete overview of the available options. You need to use Process ID
1
, asjcmd
has confirmed this is the ID of the running application inside the Docker.jcmd 1 JFR.start duration=30s settings=profile filename=/tmp/recording.jfr 1: Started recording 1. The result will be written to: /tmp/recording.jfr -
Close the shell:
exit
Recording with jcmd via Kubernetes
Use the same approach with the jcmd
tool in a Kubernetes environment if you have access to the system.
-
Identify the pod and the container in which you want to start the JFR recording with the following commands:
-
kubectl get pods
: list all pods. -
kubectl describe pod <pod-name>
: describe a specific pod and its containers.
-
-
Start a recording whenever needed:
kubectl exec -n namespace pod -- jcmd 1 JFR.start duration=180s filename=/tmp/my_recording.jfrSee JFR Command Line Options for a complete overview of the available options.
Recording at JVM Startup
For detailed performance analysis, you might prefer to start a JFR recording directly when launching a Java process.
To start recording from JVM startup, add -XX:StartFlightRecording
to the application’s Java command line. Use a comma "," for separation when using multiple options. For example, use a configuration like this in your Dockerfile
:
$ CMD ["java", "-XX:StartFlightRecorder=duration=200s,filename=/tmp/recording.jfr", "-jar", "app.jar"]
See JFR Command Line Options for a complete overview of the available options.
Recording with Azul Mission Control
Use Azul Mission Control to connect directly to the application via JMX.
-
Make sure the container is configured to allow a remote connection, as described on Prepare the Remote JVM.
-
If you are using Kubernetes, contact an administrator to set up a connection between your computer and the configured port in the container.
-
Configure a new connection in Azul Mission Control as described on Connect to a remote JVM.
-
Start a JFR recording in Azul Mission Control as described here.
-
Azul Mission Control stores the
.jfr
recording file on the system it’s running on.