Optimize your Docker Images

Docker Image Optimization

Why Should we Optimize Images

If the image size is big, then pulling and building the image will take time. And in some cases if the application is deployed on Edge at a remote place where bandwidth is less, then pulling and building a large docker image will be an issue.

Choosing the Optimized base image

Removing the layers

Each step is Dockerfile forms a new layer on the image, and each new layer adds to the build execution time and increases the size of the image.

FROM nvidia/cuda:11.2.0-runtime-ubuntu20.04
RUN apt-get update
RUN apt-get install python3.8 git -y
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get install python3-pip -y
FROM nvidia/cuda:11.2.0-runtime-ubuntu20.04
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3.8 git ffmpeg libsm6 libxext6 python3-pip && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Make Use of Caching

Docker stores the cache of each layer of a build, which is helpful when we build same image again and again with slight modification in Dockerfile.

FROM nvidia/cuda:11.2.0-runtime-ubuntu20.04
RUN apt-get update
RUN apt-get install python3.8 git -y
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get install python3-pip -y
WORKDIR /app
COPY . .
FROM nvidia/cuda:11.2.0-runtime-ubuntu20.04
WORKDIR /app
COPY . .
RUN apt-get update
RUN apt-get install python3.8 git -y
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get install python3-pip -y

Install required packages only

Every Docker image consists of multiple packages that needs to be installed. These package usually takes time to install and also adds to the overall space. We should make sure to install only the required packages.

FROM nvidia/cuda:11.2.0-runtime-ubuntu20.04
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3.8 git ffmpeg libsm6 libxext6 python3-pip python3-scipy && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Use .dockerignore files

Docker can ignore the files present in the working directory if configured in the .dockerignore file. This not only speeds up Docker image building but this can also make the image smaller. This avoids that you accidentally copy large files or folders into the image, which you don’t need to run your application.

Use multistage Docker builds

The multi-stage option in Docker can help you to optimize your images. With this feature, we can split the build in two or more stages-

  1. A run image where we copy our application code and other compiled packages from build image in the run image.
FROM node:16 
COPY . .
RUN npm install
EXPOSE 3000
CMD [ "node", "index.js" ]
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"]

Using Third Party apps to Optimize Docker Images

Docker-squash

docker-squash is a Python-based tool that squashes the last N layers of an image to a single layer. Squashing is useful to reduce your Docker image size, in case there are layers in which you created many (large) files or folders, which you then deleted in a newer layer.

Dive

Dive is a tool for exploring a docker image, layer contents, and discovering ways to shrink the size of your Docker/OCI image.

Docker Slim

DockerSlim is a tool for developers that provides a set of commands (build, xray, lint and others) to simplify and optimize your developer experience with containers. It makes your containers betters, smaller and more secure.

Summary

It is always best to optimize your Docker image as much as possible. It helps in faster deployment of applications. You can follow the above methods to go through your Dockerfile and implement the best practices needed for creating an image.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store