Docker Multi-stage
- A multi-stage build have a single dockerfile with more then one
frominstruction. - last
fromwill be main image - alas name for stages
- multiple stages will suppourted
--copy-filesto main image.-
- it will be reduce my docker image size
what problem its sloved
- one fat image:
- it will contains all packages and build tools and logs , testcases …etc
- everything in single image
- Build pattern (multiple docker files)
-
Dockerfile.build : install pre-requests and build code
-
cp required files to local or remote
-
copy above files to new docker image (Dockerfile)
-
java ==> amazoncorretto (os amazon linux2)
-
alpine ==> linux kernerl light weight os
Ex:
git clone https://github.com/spring-projects/spring-petclinic.git
FROM ubuntu AS source-code
WORKDIR app
RUN git clone https://github.com/spring-projects/spring-petclinic.git
FROM maven:3.8.3-openjdk-17 AS builder
COPY --from=source-code app/spring-petclinic spring-petclinic
WORKDIR spring-petclinic
RUN mvn clean package -DskipTests
FROM openjdk:17.0.2-jdk AS prod
ENV JAVA_HOME=/usr/jvm/openjdk-17-jdk/bin/
WORKDIR app
COPY --from=builder spring-petclinic/target/*.jar app.jar
COPY --from=source-code app/spring-petclinic/pom.xml pom.xml
CMD ["java", "-jar", "app.jar"]
Task:
- create multistage build for nop
- refer here manuall steps
- refer_source_code
