How to reduce docker Image?

How to reduce docker Image?

Docker is an open-source platform that enables developers to build, package, and deploy applications as containerized environments. Docker containers are lightweight, portable, and self-contained, allowing applications to run consistently across different computing environments, from development to production.

Docker has become an essential tool for modern software development, as it helps streamline the deployment process, reduces infrastructure costs, and improves the reliability and scalability of applications.

one of the biggest problem is docker is image size. we can use followings methods to reduce docker image size

  1. Use a smaller base image: Start with a smaller base image, such as Alpine Linux or BusyBox, instead of a full-fledged operating system like Ubuntu or CentOS.
  2. Minimize the number of layers: Each instruction in a Dockerfile creates a new layer in the image. Try to minimize the number of layers by combining multiple instructions into a single RUN command.
  3. Remove unnecessary files: Use the .dockerignore file to exclude unnecessary files from the build context, which can reduce the size of the image.
  4. Use multi-stage builds: Multi-stage builds can help reduce the final image size by allowing you to build intermediate images that only contain the necessary files and dependencies for each stage.
  5. mount volumes for static image file: Application might contains hundreds and thousands of images
  6. Use smaller dependencies: Use smaller versions of libraries and packages in your application to reduce the size of the dependencies required.

USING SMALL BASE IMAGE

We have a docker file with alpine base image. It has image size 39.9MB only

FROM alpine:3.14
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]        

when we used ubuntu as base image The final image size become 154MB

FROM ubuntu:20.04
RUN apt-get update \
    && apt-get install -y --no-install-recommends mysql-client \
    && rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["mysql"]        

Minimize the number of layers

we installed packages in different line in dockerfile using RUN it which will create different layers. we got images size 68.4MB

FROM alpine:3.14
RUN apk add --no-cache mysql-client
RUN apk add nano
RUN apk add vim
RUN apk add net-tools
RUN apk add nginx
RUN echo "I am Testing the image"
ENTRYPOINT ["mysql"]        

when we combine package installation using RUN in single line in dockerfile It will create single layer for this RUN we got images size 68.2MB

FROM alpine:3.14
RUN apk add --no-cache mysql-client nano vim net-tools nginx
RUN echo "I am Testing the image"
ENTRYPOINT ["mysql"]        

USING Multistage build

when we use normal dockerfile. we got image size of 1.05GB

FROM maven:3.8.5-openjdk-1
WORKDIR /app
COPY . /app
RUN mvn clean install -DskipTests
EXPOSE 9091
CMD ["java","-Dlog4j2.formatMsgNoLookups=true","-jar","/app/target/revalu2-email-service-0.0.1-SNAPSHOT.jar"]        

When we use multistage build. We got final image size 538MB. which is almost 50% less in size

FROM maven:3.8.5-openjdk-17? AS builde
WORKDIR /app
COPY . /app
RUN mvn clean install -DskipTests

FROM openjdk:17.0.2-jdk
WORKDIR /root/
RUN mkdir ReportPath
COPY --from=builder /app/target/revalu2-email-service-0.0.1-SNAPSHOT.jar .
EXPOSE 9091
CMD ["java","-Dlog4j2.formatMsgNoLookups=true","-jar","/root/revalu2-email-service-0.0.1-SNAPSHOT.jar"]        


#devops #docker #kubernetes Hyperleap Future Technologies #containers

Great post! Thanks for sharing your tips on reducing docker image size. Being mindful of the layers and base images used can make a big difference when it comes to optimizing Docker containers.

回复

要查看或添加评论,请登录

Kashif Mehmood的更多文章

  • AWS SSM for Bastion host

    AWS SSM for Bastion host

    Many IT companies already using cloud providers (like Amazon AWS, Google GCP or Microsoft Azure) . A lot of IT…

  • Monitor websites with Prometheus, Granfa and Blackbox

    Monitor websites with Prometheus, Granfa and Blackbox

    I am assuming that you have already setup Prometheus and Grafana Lets start with blackbox exporter installation and…

    2 条评论
  • Performance test using k6

    Performance test using k6

    K6 is a popular open-source load testing tool designed for developers and DevOps teams to create and execute…

社区洞察

其他会员也浏览了