Multi-staged docker image build
-
Java:
- To build java application i might need
- JDK
- Maven/Gradle
- To run the java application i need
- JRE
- To build java application i might need
-
Dotnet:
- To build dotnet application i need
- dotnet SDK
- To run the dotnet application i need
- dotnet Runtime
- To build dotnet application i need
- To solve this docker introduced multi staged Docker file
- i.e in Dockerfile i can have multiple stages, only last stage will be your application image
- Example
FROM maven:3-eclipse-temurin-21 AS build
COPY . /app
WORKDIR /app
RUN mvn package
FROM eclipse-temurin:21-jre-alpine
RUN mkdir /app
COPY --from=build /app/target/spring-petclinic-4.0.0-SNAPSHOT.jar /app
WORKDIR /app
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-4.0.0-SNAPSHOT.jar"]
- Try for similar stuff in other languages,
- Exercise: Refer Here and Refer Here for other languages
Securing docker images
- Docker gives docker hardened images (DHI) and there are distroless images
