How to Profile Java Applications in Docker Containers with JFR

Transcript:

Hi friends! In this video, I will show you how to profile Java applications in Docker containers using Java Flight Recorder. Java Flight Recorder (JFR) is a low-overhead profiling and diagnostic tool built into the OpenJDK, so you don't have to install it manually. We can profile the application right from the start, attach to a running application, or observe it in JDK Mission Control.

First, let's look at how to start a JFR recording at application startup. You can take any application—I'm using Spring PetClinic. The Dockerfile includes Liberica JDK Lite and minimalistic Alpaquita Linux. The most important option to enable JFR is ‘StartFlightRecording’, where you can set parameters like duration and filename. After building and running the container, you can retrieve the recording file from the ‘tmp’ directory using docker cp. The recorded data can be analyzed in JDK Mission Control, an open-source tool.

Now, let's see how to attach to a running container and profile it. Instead of enabling JFR at startup, we can use the ‘jcmd’ tool, which is part of the JDK. However, production images usually contain only the JRE, not the full JDK. To solve this, we use ephemeral containers—temporary containers that include the JDK and attach to the running application. By running an ephemeral container with ‘pid=container’, we can use ‘jcmd’ to start a JFR recording. After profiling, we retrieve the file with docker cp without restarting the main container.

Lastly, if you want to monitor a running application in real time, JDK Mission Control allows remote profiling. We modify the Dockerfile to enable remote JVM connections using JMX-related options like ‘jmxremote.port’ and ‘jmxremote.rmi.port’. After rebuilding and running the container with these options, we connect to the JVM in Liberica Mission Control. This lets us observe real-time performance metrics.

In this video, we covered how to profile Java applications in containers using Java Flight Recorder. In a future video, we will explore profiling containerized Java apps with Async Profiler, another low-overhead profiler. Don't forget to like this video, subscribe to our channel, and see you next time!

Summary

This video explains how to profile Java applications in Docker containers using Java Flight Recorder (JFR), a built-in OpenJDK tool. It covers three profiling methods: enabling JFR at application startup, attaching to a running container using an ephemeral container with `jcmd`, and monitoring real-time performance with JDK Mission Control via remote JVM connections. Step-by-step instructions are provided for configuring Dockerfiles, retrieving profiling data, and analyzing application performance.

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Videos
card image
Aug 6, 2025
GraalVM for Java Developers: The Ultimate Beginner’s Guide

What is GraalVM and how can it improve your Java applications? In just 10 minutes, this video explains the three main components of GraalVM — the JIT compiler, Native Image, and Polyglot API. Learn how to boost performance, reduce startup time, and combine multiple languages in one app. Whether you’re building microservices, serverless apps, or just exploring modern JVM tooling, this is your quick-start guide to GraalVM.

Videos
card image
Jul 15, 2025
Java Downgrade Challenge: From JDK 8 to 1.1 (Part 2)

In Part 2 of the Java Downgrade Challenge, we continue our journey — now from Java 8 all the way to Java 1.1. No streams, no lambdas, no generics, no collections — and at one point, we even boot up Windows 98. If you thought Part 1 was painful, this one unwinds Java history line by line. By the end, the familiar Java from today will be almost gone.

Further watching

Videos
card image
Aug 27, 2025
Buildpacks for Spring Boot

Buildpacks for Spring Boot: no Dockerfiles, no hassle — just production-ready container images in one command. Tired of maintaining Dockerfiles? In this tutorial, you’ll learn how to use buildpacks to create optimized Spring Boot containers — fast, secure, and cloud-ready — with just one command. We’ll show what happens under the hood: automatic dependency detection, layered image creation, memory tuning, SBOM generation, and how to tweak builds with just a few plugin options. Need faster startup, smaller image size, or JFR monitoring? Buildpacks can handle it — and we’ll show you how.

Videos
card image
Aug 20, 2025
Flyway in Spring Boot: Step-by-Step tutorial with Maven

Learn how to use Flyway in Spring Boot with Maven for smooth and reliable database migrations. In this hands-on tutorial, we cover everything from setting up PostgreSQL in Docker, configuring Flyway in your application, writing versioned and repeatable migrations, to using Flyway in CI/CD pipelines with GitHub Actions. Whether you’re new to Flyway or want to master schema version control in Spring Boot, this video will guide you step by step.

Videos
card image
Aug 14, 2025
Stop Using DTOs – A Cleaner Way for Your Java APIs

Still creating DTOs for every API in your Spring Boot project? You might be overcomplicating things. In this video, we show why DTOs aren’t always necessary and how to replace them with @JsonIgnore, @JsonView, and Jackson Mixins. You’ll see real examples of hiding sensitive fields, creating role-based views, and cutting boilerplate — all while keeping your API safe, clean, and easy to maintain.