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?
How to Make Docker Image Smaller in Size?
Let us find out the different methods and strategies to make the Docker image smaller in size.
1. Use a Smaller Base Image
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
2. Use Multi-Stage Builds
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”]
3. Minimize Layers
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/*
4. Remove Unnecessary Files
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/*
5. Use .dockerignore
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
6. Optimize Dependency Installation
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
7. Compress Files
Another good way is to compress large files before adding them to the final image.
Example:
ADD my-large-file.tar.gz /app/
8. Leverage Caching
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.
9. Use Distroless Images
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”]
10. Remove Unused Dependencies
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/*
11. Use Docker Slim or Dive
Tools like Docker Slim or Dive can analyze and optimize your Docker images. Example with Docker Slim:
docker-slim build –target my-image
12. Avoid Installing Debugging Tools in Production
You should not install debugging tools like vim, nano, or strace, as they can significantly increase image size. Avoid including them in production images.
13. Use Static Binaries
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 .
14. Regularly Update and Prune Images
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.