Docker ClassRoom Notes- Multistage builds -04/Oct/2019

Multi Stage Builds

  • Used for Building docker images with optimization.

Scenario1: I need a spring pet clinic image

  • Build server has only docker installed
  • Steps:
    • Clone the spring petclinic
    • build the spring petclinic project
    • copy the jar file to some folder
    • Execute the command to start spring petclinic
  • Summarize:
git clone https://github.com/spring-projects/spring-petclinic.git
mvn package
cp ./target/spring-petclinic*.jar /spring-petclinic.jar
java -jar spring-petclinic.jar
  • Without multistage builds your dockerfile will be
FROM openjdk:8
# RUN install maven
# RUN install git
# RUN mvn package
# RUN copy jar files
EXPOSE 8080
CMD 
  • In the cases like above use multi staged docker build
  • In one Dockerfile
    • you will have multiple FROM instructions
FROM maven
RUN git clone
RUN mvn package

FROM openjdk:8
RUN copy the file from above stage (maven) into this image
  • Refer Here for multistage builds

  • The resulting Dockerfile with multi-stage build will look something like this

FROM maven:3-jdk-8 as mvn
RUN git clone https://github.com/spring-projects/spring-petclinic.git
RUN cd spring-petclinic && mvn package

FROM openjdk:8-alpine
LABEL AUTHOR="khaja"
COPY --from=mvn /spring-petclinic/target/spring-petclinic*.jar /spring-petclinic.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar"]
CMD ["/spring-petclinic.jar"]
  • Scenario-2: Gameoflife
FROM maven:3-jdk-8 as mvn
RUN git clone https://github.com/wakaleo/game-of-life.git
RUN cd game-of-life/ && mvn package

FROM tomcat:8
LABEL AUTHOR="khaja"
COPY --from=mvn /game-of-life/gameoflife-web/target/gameoflife*.war /usr/local/tomcat/webapps/gameoflife.war
EXPOSE 8080

Docker Persistence for Stateful Applications

  • Volumes
  • Bindmounts
  • tmpfs
  • namedpipe

4 thoughts on “Docker ClassRoom Notes- Multistage builds -04/Oct/2019

  1. Could you please explain below questions,

    In previous session to achieve output of spring-petclinic directly openjdk & cmd instructions are written in dockerfile, but now why we require maven & git spring-petclinic. if two FROM instructions or two base images are used in writing dockerfile then the size should increase right?

    RUN git clone https://github.com/spring-projects/spring-petclinic.git
    1, In above Instruction, what is happening on git clone, I have seen in given url location there are some files & folders, so where these files and folders are cloned to ?

    RUN cd spring-petclinic && mvn package
    1, Initially for maven the directory should be root and there is no instruction to mkdir than how spring-petclinic directory is created ?
    2, In above instruction, what mvn package do ?

    FROM openjdk:8-alpine
    1, what does openjdk8-alpine, is it like we are pulling layers of alpine along with openjdk?

    COPY –from=mvn /spring-petclinic/target/spring-petclinic*.jar /spring-petclinic.jar
    1, how /spring-petclinic/target/ directories are created and how spring-petclinic*.jar placed in the location and what does the ashtrick represents?

      1. HI sir,
        I got answer for my first point. Could you please explain below questions too.

        RUN git clone https://github.com/spring-projects/spring-petclinic.git
        1, In above Instruction, what is happening on git clone, I have seen in given url location there are some files & folders, so where these files and folders are cloned to ?

        RUN cd spring-petclinic && mvn package
        1, Initially for maven the directory should be root and there is no instruction to mkdir than how spring-petclinic directory is created ?
        2, In above instruction, what mvn package do ?

        FROM openjdk:8-alpine
        1, what does openjdk8-alpine, is it like we are pulling layers of alpine along with openjdk?

        COPY –from=mvn /spring-petclinic/target/spring-petclinic*.jar /spring-petclinic.jar
        1, how /spring-petclinic/target/ directories are created and how spring-petclinic*.jar placed in the location and what does the ashtrick represents?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner