What is the docker multi-stage build?

What is the docker multi-stage build?

Although Docker eliminates the need for dependencies on the client system, inefficient building of the image can result in a heavy image size. Many developers are unaware of this feature, leading to high data transfer costs for their organization. In this blog, I will demonstrate how to use Docker's multi-stage build to comprehensively build and reduce the image size.

By utilizing Docker multi-stage builds, you can create more efficient and simplified container images by consolidating multiple build stages into one Docker file. This feature was introduced in Docker 17.05 and is particularly useful for applications that require build processes or dependencies that are not necessary for the final runtime image. Here's how Docker multi-stage builds work:

Multiple Build Stages:

You define multiple build stages in your Dockerfile, each with its own base image and set of instructions. Each stage is like a separate container image during the build process.

Copy Artifacts:

You can copy files or artifacts from one build stage to another using the COPY --from=<stage> instruction. This allows you to selectively include only the necessary files in the final image.

Reduce Image Size:

By using multi-stage builds, you can avoid including build tools, development dependencies, and intermediate files in the final container image. This results in smaller and more efficient images that are easier to distribute and deploy.

Tip

If you're new to Docker, start by reading these posts before continuing.

Here's how you can effectively use multi-stage builds in Docker:

  1. Create a Dockerfile with Multiple Stages:

    To use multi-stage builds, create a Dockerfile that defines multiple stages. Each stage represents a step in the build process and can have its own base image and set of instructions.

    # Stage 1: Build the application
    FROM node:14 AS builder
    WORKDIR /app
    COPY package.json package-lock.json ./
    RUN npm install
    COPY . .
    RUN npm run build
    
    # Stage 2: Create the production image
    FROM nginx:alpine
    COPY --from=builder /app/build /usr/share/nginx/html
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
    

    In this example, there are two stages: one for building a Node.js application and another for creating an Nginx-based production image.

  2. Use Different Base Images:

    Each stage can use a different base image that is suitable for the specific task. In the example above, the first stage uses a Node.js base image for building the application, and the second stage uses an Nginx base image for the production image.

  3. Copy Artifacts Between Stages:

    To copy artifacts (e.g., compiled code or static files) from one stage to another, use the COPY --from instruction, as shown in the example. This allows you to keep only the necessary files in the final image.

  4. Keep Only What's Necessary:

    In the final stage, include only the necessary files and dependencies needed to run your application. This helps reduce the size of the container image and minimize security risks.

  5. Build the Docker Image:

    To build the Docker image, use the docker build command with the -t flag to specify a tag for the image. For example:

    docker build -t myapp:v1 .
    
  6. Run the Docker Container:

    Once the image is built, you can run a container from it:

    docker run -p 8080:80 myapp:v1
    

    This will start your application in a container.

Using multi-stage builds helps create more efficient and smaller Docker images, making them easier to distribute and deploy. It's particularly useful for building applications that require compilation or build steps, as it allows you to keep only the necessary artifacts in the final image while discarding build tools and intermediate files.

Note

Docker does not have a built-in concept of "stages" in the same way as some other containerization or build systems like multi-stage builds in Docker can help you create smaller and more efficient container images by reducing the image size and minimizing the number of dependencies.

Happy Dockerizing! 🐋 See you in next post. Don't forget to comment your thoughts for this post. Share knowledge with others...

Let's Connect on:

Share :

Related Posts

A Step-by-Step Guide to Saving AWS Costs by Uploading Public Docker Images to Private ECR

A Step-by-Step Guide to Saving AWS Costs by Uploading Public Docker Images to Private ECR

In the world of containerized applications, Docker Hub is a popular choice for storing and sharing Docker images. While Docker Hub is a convenient option, it may not be the most cost-effective choice

read more
Kubernetes: The Future of Cloud Computing

Kubernetes: The Future of Cloud Computing

In the world of modern software development and deployment, Kubernetes has emerged as a powerhouse that is changing the way we manage and scale applications. Whether you are a seasoned developer or a

read more
Basic Docker Commands CheatSheet

Basic Docker Commands CheatSheet

Docker has become an indispensable tool in the world of software development and deployment. It enables developers to create, deploy, and run applications in lightweight, portable containers. Whether

read more
From Dockerfile to Docker Compose: A Comprehensive Guide in Containerization

From Dockerfile to Docker Compose: A Comprehensive Guide in Containerization

Containerization has revolutionized the way we develop, deploy, and manage applications. Docker has been at the forefront of this containerization movement, simplifying the process of packaging an ap

read more
How to inspect the docker image?

How to inspect the docker image?

When it comes to Microservice architecture, the docker image is like the soul of the system. It is essential to understand the purpose of a docker image and what it contains so that you can use the r

read more
OOPS Concept in Simple English with Examples

OOPS Concept in Simple English with Examples

OOP is a way of writing computer programs that makes it easier to manage and organize your code. Let's break down some key OOP concepts: 1. Class: Think of a class as a blueprint or a templ

read more
What are Micro-service and Monolithic Architecture?

What are Micro-service and Monolithic Architecture?

Introduction: In the world of software development, architecture plays a crucial role in determining the scalability, maintainability, and flexibility of an application. Two popular architectu

read more
What is Docker and its use case?

What is Docker and its use case?

Docker is a powerful tool that has revolutionized the way applications are deployed and managed. It is an open-source platform that allows developers to build, package, and deploy applications in a c

read more
A Comprehensive Guide to Docker Swarm: Orchestrate Your Containers with Ease

A Comprehensive Guide to Docker Swarm: Orchestrate Your Containers with Ease

In the world of containerization and microservices, managing and scaling containers efficiently has become a critical task. Docker Swarm, a native clustering and orchestration solution for Docker con

read more