What is the docker multi-stage build?
Tags: docker, kubernetes, containers, programming
Published: 2023-09-19 07:01:07


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.

<Notice type="tip">
If you're new to Docker, start by reading these posts before continuing.
</Notice>

- [What is Docker and its use case?](https://sarvesh.xyz/blog/what-is-docker-and-its-use-case)
- [Basic Docker Commands CheatSheet](https://sarvesh.xyz/blog/docker-basic-commands-cheatsheet)
- [How to inspect the docker image?](https://sarvesh.xyz/blog/how-to-inspect-the-docker-image)

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.

   ```Dockerfile
   # 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:

   ```bash
   docker build -t myapp:v1 .
   ```

6. **Run the Docker Container:**

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

   ```bash
   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.

<Notice type="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.
</Notice>

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