Optimize Docker Images: Practical Guide How to Reduce Docker Image Size?

Optimize Docker Images: Practical Guide How to Reduce Docker Image Size?

- 7 mins

Optimize Docker Images: Practical Guide

Introduction

Optimizing Docker images is crucial for reducing size, improving build times, and enhancing security. A typical Docker image consists of a base image, dependencies/files/configs, and unwanted software (cruft). This guide explores effective methods to optimize Docker images with practical examples.


Methods to Optimize Docker Images

1. Use Minimal Base Images

Minimal base images reduce the size and security risks of Docker images. Examples include:

Example:

FROM alpine:latest

Note: Use enterprise-approved base images for production to ensure security compliance.


2. Use Multistage Builds

Multistage builds separate build and runtime environments, reducing the final image size.

Example: Multistage Dockerfile

# Stage 1: Build
FROM node:16 as build
WORKDIR /app
COPY package.json index.js env ./
RUN npm install

# Stage 2: Runtime
FROM node:alpine
COPY --from=build /app /
EXPOSE 8080
CMD ["node", "index.js"]

Results:

docker-ima

FROM node:16

COPY . .

RUN npm installEXPOSE 3000

CMD [ "node", "index.js" ]
$ docker build -t test/node-app:1.0 --no-cache -f Dockerfile1 .

docker-ima

FROM node:16 as build

WORKDIR /app
COPY package.json index.js env ./
RUN npm install

FROM node:alpine as main

COPY --from=build /app /
EXPOSE 8080
CMD ["index.js"]
$ docker build -t test/node-app:2.0 --no-cache -f Dockerfile1 .

docker-ima


3. Minimize Layers

Each RUN, COPY, and FROM instruction creates a new layer. Combining instructions minimizes layers and reduces size.

Example: Unoptimized Dockerfile

Optimized Dockerfile:

Results:

  1. We need to create daemon.json file under /etc/docker/ and it should contains
    {
      "experimental": true
    }
    
  2. We need to export DOCKER_BUILDKIT=1 to env
    export DOCKER_BUILDKIT=1
    
  3. We can now build an image and analysis.
time docker build -t test/optimize:3.0 --no-cache -f Dockerfile3 .

Result: docker-ima

docker-ima


4. Leverage Caching

Good Example:

FROM ubuntu:latest
RUN apt-get update -y && apt-get install -y vim
COPY . .

Less Optimal Example:

FROM ubuntu:latest
COPY . .
RUN apt-get update -y && apt-get install -y vim

Why? The second example invalidates the cache more frequently.


5. Use Dockerignore

Use a .dockerignore file to exclude unnecessary files from the build context, reducing the image size and preventing cache invalidation.

Example: .dockerignore File

node_modules
*.log
.env

6. Keep Application Data Elsewhere

Avoid storing application data in images. Use Docker volumes or Kubernetes persistent volumes to manage data externally, keeping images lightweight.


Docker Image Optimization Tools

Here are some tools to help optimize Docker images:


Summary

By using minimal base images, multistage builds, minimizing layers, leveraging caching, and other best practices, you can create optimized Docker images. Integrating tools like Dive and SlimToolkit into your CI/CD pipeline ensures consistent optimization. For security, scan images with tools like Trivy before deployment.



Thanks for reading…





:+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1:


Guneycan Sanli.


Guneycan Sanli

Guneycan Sanli

A person who like learning, music, travelling and sports.

comments powered by Disqus