Showing posts with label stage. Show all posts
Showing posts with label stage. Show all posts

Saturday, March 12, 2022

EXAMPLES: Dockerfile MultiStage file




Docker MultiStage file

THIS IS UNI STAGE DOCKER FILE

Step1:-
Download a sample git maven project for this example

    git clone https://github.com/onlineTrainingguy/jenkinscicd.git

Step2:-
go to jenkinscicd dir

    cd jenkinscicd

Step3:-
Modify Dockerfile content as given below

    FROM maven:latest
    WORKDIR /
    COPY src /src
    COPY pom.xml /
    RUN mvn clean package
    CMD java -cp /target/myproj-1.0-SNAPSHOT.jar com.raman.App



Step4:-
Build the image using docker build command

    docker build . -t unistagemavenimg

Step 5:-
Check the image size it will approximate 700 MB+

    docker images unistagemavenimg

Step 6:-
Create container with above image and check the output

    docker run -it unistagemavenimg

output will be
Maven Job is running on Build Server! (but if you following this steps, you will get a different output. why? because in jenkinscicd/src/main/java/com/raman/App.java, the app.java file has a different output. you can check by vi into it and even edit it to your liking!)



Now lets create multi stage Docker file
Step 1: Modify Dockerfile content as given below (notice the AS build)

    FROM maven:latest AS build
    WORKDIR /
    #copy from localfile system to docker file system
    COPY src /src
    COPY pom.xml /
    RUN mvn clean package

    #(if above Dockerfile haven't been run before, include below as well) 
    CMD java -cp /target/myproj-1.0-SNAPSHOT.jar com.raman.App

    FROM openjdk:alpine
    COPY --from=build /target/myproj-1.0-SNAPSHOT.jar /myproj-1.0-SNAPSHOT.jar
    EXPOSE 8080
    CMD java -cp /myproj-1.0-SNAPSHOT.jar com.raman.App



Step 2:- Build image using docker build command

docker build . -t multistageimg

Step 3:- Check the image size it will approximate 100 MB

docker images multistageimg

Step 4:-Create container with above image and check the output

docker run multistageimg

output will be same but the image size is reduced
Maven Job is running on Build Server!


Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...