Docker Image Optimization: Shrinking Your Container's Waistline

Docker Image Optimization: Shrinking Your Container's Waistline

Creative Tips and Tricks to Reduce Your Docker Image Size

Table of contents

Introduction

Docker Tip #3: Chain Your Docker RUN Instructions to Shrink Your Images —  Nick Janetakis

Docker images are essential to containerization, but they can quickly become bloated with unnecessary files and dependencies. The bigger the image, the longer it takes to pull and run, which can be a headache for developers and users alike. So, let's dive into some ways to make your Docker images leaner and meaner.

Steps

  1. Use multi-stage builds

Multi-stage builds allow you to use multiple Dockerfiles in a single build. By breaking your build into smaller pieces, you can significantly reduce the size of your final image. Think of it like Marie Kondo-ing your Docker build process.

Here's an example Dockerfile that uses multi-stage builds to reduce the size of a Node.js image:

# Build stage
FROM node:14.17.6-alpine AS build
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

# Production stage
FROM node:14.17.6-alpine
WORKDIR /app
COPY --from=build /app/dist /app
CMD ["node", "app.js"]

This Dockerfile first builds the Node.js app in one stage, and then copies only the built artifacts to the production stage. This approach helps keep the final image size small.

  1. Minimize dependencies

The more dependencies your Docker image has, the bigger it will be. One way to minimize dependencies is to use a smaller base image. For example, you could use Alpine Linux instead of Ubuntu or Debian as your base image. Alpine Linux is a lightweight Linux distribution that has a much smaller footprint than other distributions.

Here's an example Dockerfile that uses Alpine Linux as the base image:

FROM alpine:3.14.2
RUN apk update && \
    apk add --no-cache nodejs npm
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "app.js"]

In this example, we're using Alpine Linux as the base image, and then only installing Node.js and npm to minimize the number of dependencies.

  1. Remove unnecessary files

When building your Docker image, be mindful of the files you're including. Do you really need that entire folder of cat memes? Probably not. Removing unnecessary files can help reduce the size of your final image.

Here's an example Dockerfile that removes unnecessary files:

FROM node:14.17.6-alpine
WORKDIR /app
COPY . .
RUN npm install && \
    npm run build && \
    rm -rf node_modules && \
    npm install --only=production && \
    rm -rf src test .dockerignore Dockerfile README.md
CMD ["node", "app.js"]

In this example, we're using the rm command to remove the node_modules folder, source code, tests, and other unnecessary files after the build is complete.

  1. Use a .dockerignore file

Like a .gitignore file, a .dockerignore file allows you to exclude files and folders from the build process. This can help reduce the size of your Docker image by excluding unnecessary files.

Here's an example .dockerignore file:

node_modules
src
test
.dockerignore
Dockerfile
README.md

In this example, we're telling Docker to ignore the node_modules, src, and test folders, as well as the .dockerignore, Dockerfile, and README.md files.

How to Use the Node Docker Official Image | Docker

In conclusion, reducing the size of your Docker images is essential

Did you find this article valuable?

Support Khushiyant by becoming a sponsor. Any amount is appreciated!