How to Build Smaller Container Images: Docker Multi-Stage Builds

Listen to this Post

Containerized solutions have become a cornerstone of modern application development, offering flexibility and scalability. However, as container images grow in size, they can become bloated and inefficient. This article explores how to build smaller container images using Docker multi-stage builds, with practical examples and commands to help you optimize your containerization process.

You Should Know:

1. Understanding Docker Multi-Stage Builds

Multi-stage builds allow you to use multiple `FROM` statements in your Dockerfile. Each `FROM` instruction can use a different base image, and only the final image is retained. This helps reduce the size of the final image by discarding intermediate layers that are no longer needed.

2. Example: Multi-Stage Dockerfile for a Node.js Application

Below is an example of a multi-stage Dockerfile for a Node.js application:


<h1>Stage 1: Build the application</h1>

FROM node:16 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

<h1>Stage 2: Create the final image</h1>

FROM node:16-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY package*.json ./
RUN npm install --production
EXPOSE 3000
CMD ["node", "dist/index.js"]

In this example:

  • The first stage (build) installs dependencies and builds the application.
  • The second stage (node:16-alpine) copies only the necessary files from the build stage and installs production dependencies.

3. Key Commands for Optimizing Docker Images

  • Use `docker build -t your-image-name .` to build your Docker image.
  • Use `docker images` to check the size of your images.
  • Use `docker history your-image-name` to inspect the layers of your image.
  • Use `docker system prune` to clean up unused Docker objects and free up space.

4. Additional Tips

  • Use lightweight base images like `alpine` or scratch.
  • Minimize the number of layers by combining commands using &&.
  • Remove unnecessary files and dependencies in the final image.

What Undercode Say:

Building smaller container images is essential for optimizing resource usage and improving deployment efficiency. By leveraging Docker multi-stage builds, you can significantly reduce the size of your images while maintaining functionality. Here are some additional Linux and IT-related commands to enhance your containerization workflow:

  • Linux Commands:
  • df -h: Check disk space usage.
  • du -sh <directory>: Check the size of a specific directory.
  • tar -czvf archive.tar.gz <directory>: Compress a directory for efficient storage.

  • Windows Commands:

  • docker ps: List running containers.
  • docker stop <container-id>: Stop a running container.
  • docker rmi <image-id>: Remove an unused image.

  • Advanced Docker Commands:

  • docker-compose up -d: Start multiple containers defined in a `docker-compose.yml` file.
  • docker exec -it <container-id> /bin/bash: Access a running container’s shell.

By following these steps and commands, you can streamline your containerization process and ensure your applications run efficiently in production environments.

Expected Output:

A smaller, optimized Docker image ready for deployment, with reduced resource consumption and faster build times.

Reference:

How to Build Smaller Container Images: Docker Multi-Stage Builds | iximiuz Labs

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image