Skip to main content

Command Palette

Search for a command to run...

How to Reduce Docker Image

Published
•2 min read•View as Markdown
How to Reduce Docker Image

The following are the methods by which we can achieve docker image optimization.

  1. Using distroless/minimal base images

  2. Multistage builds

  3. Minimizing the number of layers

  4. Understanding caching

  5. Using Dockerignore

  6. Keeping application data elsewhere

🔹Create One Ec2 instance i.e t2 micro.

🔹First Clone this project:-

git clone https://github.com/kumarvimal349/springboot-crud-k8s.git

🔹Command :- ls

🔹Install docker-engine on your Instance:-

Command:-

apt-get install docker.io -y 
apt-get update -y

🔹Build Dockerfile ( without MultiStage):-

FROM openjdk:8
EXPOSE 8080
ADD target/springboot-crud-k8s.jar springboot-crud-k8s.jar
ENTRYPOINT ["java","-jar","/springboot-crud-k8s.jar"]

Then run command :- docker build . -t spring-app

🔹Command :- Docker Images

you see images size is 565MB which is very high, So we can reduce this image size..

🔹Build Dockerfile ( with MultiStage):-

############# BASE IMAGE #################################
FROM openjdk:8 as build
WORKDIR /app
COPY . /app
############# MULTI STAGE IMAGE #################################
FROM openjdk:8-jdk-alpine as main
COPY --from=build /app /
EXPOSE 8080
ADD target/springboot-crud-k8s.jar springboot-crud-k8s.jar
ENTRYPOINT ["java","-jar","/springboot-crud-k8s.jar"]

Then run command :- docker build . -t spring-app-multistage

🔹Command :- Docker Images

Image size is reduced to 217 MB..

I hope this helps! Let me know if you have any questions.

I am Vimal kumar, Please do follow me here and support #devOps #trainwithshubham #github #devopscommunity #devops #cloud #devoparticles #docker #kubernetes #aws #kubeWeek

Vimal Kumar

Shubham Londhe