Everybody wants to reduce the size of their Docker image. This is crucial because reducing the size of the Docker image will improve performance, build times, deployment speed, and storage consumption. Let us find out how to make a Docker image smaller in Size.
You may also like: How do you find out why your server is slow?
Let us find out the different methods and strategies to make the Docker image smaller in size.
We have to choose a minimal base image like alpine or distroless, which are lightweight (~5MB) instead of larger ones like Ubuntu or Debian. For example:
FROM alpine: latest
Another popular method to get a smaller docker image is using Multi-stage builds. Multi-stage builds allow you to make multiple stages with each stage separated. For example, we can have different stages for build and runtime environments. This makes sure that only the required files are included in the final docker image.
Example:
# Stage 1: Build
FROM golang:1.45 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp .
# Stage 2: Runtime
FROM alpine: latest
WORKDIR /app
COPY –from=builder /app/myapp .
CMD [“./myapp”]
We can try to minimize the Docker file layers. We can combine multiple RUN commands into a single command to reduce the number of layers. Example:
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
Instead of:
RUN apt-get update
RUN apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*
To minimise the docker image size, we need to get rid of the temp files or caches after installation. Example:
RUN apt-get update && \
apt-get install -y curl && \
rm -rf /var/lib/apt/lists/*
Another unknown method is to create a .dockerignore file. This file will exclude unnecessary files (e.g., node_modules, .git) from being copied into the image.
Example Edit the .dockerignore file:
node_modules
.git
*.log
You should only install the dependencies you need for production and not all the unnecessary ones.
For example, in Node.js:
RUN npm install –only=production
Another good way is to compress large files before adding them to the final image.
Example:
ADD my-large-file.tar.gz /app/
Caching is a method to make the file accessible faster without going to the source. In Docker, you can use a Dockerfile to utilise the layer caching.
By layer caching we mean that you should put less frequently changing instructions (e.g., dependency installation) before frequently changing ones (e.g., copying source code). This makes sure every build after the first build uses the caching feature, and no need to run the instructions again and again, thereby saving the running time.
Google’s distroless images provide a minimal runtime environment with no shell or package manager. Example:
FROM gcr.io/distroless/base-debian11
COPY myapp /app
CMD [“/app/myapp”]
Uninstall unnecessary packages after installation. Example:
dockerfile
Copy
RUN apt-get update && \
apt-get install -y curl && \
apt-get remove -y curl && \
rm -rf /var/lib/apt/lists/*
Tools like Docker Slim or Dive can analyze and optimize your Docker images. Example with Docker Slim:
docker-slim build –target my-image
You should not install debugging tools like vim, nano, or strace, as they can significantly increase image size. Avoid including them in production images.
For compiled languages like Go or Rust, use static binaries to avoid including shared libraries in the image. Example for Go:
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
Regularly clean up unused images and layers using the below command:
docker image prune -a
By following the above mentioned techniques, you can significantly reduce your Docker image size and improve build times and deployment speed. We hope this article helped you on How to Make Docker Image Smaller in Size.
You can also utilise the Docker official documentation. We always recommend referring to the Docker official documentation.
iPhone Deal Alert: Massive Price Drop Makes Premium iPhone Available Under ₹70,000 INR - Will…
You glance at your smartphone about 58 times daily - but did you know this…
Get ready, smartphone lovers! Lava International is about to launch its new smartphone, the Lava…
You often ask yourself, “Why is the server so slow today?” Whether it's your application…
The Google Pixel 9 Pro Fold is the latest foldable phone in the Google Pixel…
In the high-stakes world of cybersecurity, leaders are often seen as pillars of calm and…