Introduction
Distroless images are minimal Docker images designed to include only the essential components required to run an application. They exclude package managers, shells, and other utilities that typically come with standard Docker images. This approach enhances security and performance by reducing the attack surface.
For a Java application, a distroless image contains only the Java Runtime Environment (JRE). Below is an example Dockerfile demonstrating how to set up a Java application using a distroless image:
# Stage 1: Build the application using Maven
FROM maven:3.8.5-openjdk-17 AS build
WORKDIR /app
COPY . .
RUN mvn clean package -DskipTests
# Stage 2: Run the application using a distroless image
FROM gcr.io/distroless/java17
COPY --from=build /app/target/myapp.jar /app/myapp.jar
CMD ["-jar", "/app/myapp.jar"]
In this Dockerfile:
- The first stage uses
maven:3.8.5-openjdk-17to compile the application. WORKDIRsets the working directory to/app.COPYbrings in all source files.RUNexecutes Maven to build the JAR, skipping tests.- The second stage uses
gcr.io/distroless/java17to run the app. COPY --from=buildpulls the JAR from the build stage.CMDspecifies the run command.
This setup ensures the final image is minimal — only what is necessary to run the application.
Why use distroless images
- Lightweight: Smaller size makes them easier to store and transport.
- Enhanced security: Fewer packages mean fewer potential vulnerabilities.
- Reduced blast radius: Fewer components limit the damage from a security compromise.
How to use
Google provides open source distroless images at https://github.com/GoogleContainerTools/distroless — pick the image appropriate for your runtime.
Distroless images shine especially when combined with ephemeral containers. Ephemeral containers differ from regular containers in that they lack resource guarantees, will never be automatically restarted, and are not appropriate for building applications — but they are ideal for debugging minimal production images without adding a shell to the image itself.
The future: chiseled images
Chiseled images expand on distroless principles by removing even more unnecessary components, optimized for specific workloads.
Benefits of chiseled images:
- Smaller size: Even smaller than distroless, easier to distribute.
- Enhanced security: Fewer components, fewer attack vectors.
- Optimized performance: Tailored for specific applications.