“Distroless” images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.
A distroless image is a slimmed down Linux distribution image plus the application runtime, resulting in the minimum set of binary dependencies required for the application to run.
A typical container consists of:
- Distro base layer - linux distribution files (Ubuntu, CentOS, Debian)
- Runtime layer (JRE for Java, Python runtime, glibc for C++)
- Application layer - actual application binaries
w
Distroless images are very small. The smallest distroless image, gcr.io/distroless/static-debian11
, is around 2 MiB. That's about 50% of the size of alpine
(~5 MiB), and less than 2% of the size of debian
(124 MiB).
The general syntax involves adding FROM
additional times within your Dockerfile - whichever is the last FROM
statement is the final base image. To copy artifacts and outputs from intermediate images use COPY --from=<base_image_number>
.
https://github.com/GoogleContainerTools/distroless/blob/main/examples/java/Dockerfile
javac HelloJava.java
jar cfe main.jar HelloJava HelloJava.class
java -jar main.jar
FROM openjdk:11-jdk-slim-bullseye AS build-env
COPY HelloJava.java /app
WORKDIR /app
RUN javac HelloJava.java
RUN jar cfe main.jar HelloJava HelloJava.class
FROM gcr.io/distroless/java11-debian11
COPY --from=build-env /app /app
WORKDIR /app
CMD ["main.jar"]
=====
FROM openjdk:11-jdk-slim-bullseye AS build-env
COPY . /app/examples
WORKDIR /app
RUN javac examples/*.java
RUN jar cfe main.jar examples.HelloJava examples/*.class
FROM gcr.io/distroless/java11-debian11
COPY --from=build-env /app /app
WORKDIR /app
CMD ["main.jar"]
==================
package examples;
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
====================
Comments
Post a Comment