Mastering Multi-Stage Builds in Docker 🚀

Recap of Previous Days

Day 1: We explored Docker fundamentals, the issues before containers, how Docker solves these problems, the differences between Docker and virtual machines, and an overview of Docker’s workflow and architecture.

Day…


This content originally appeared on DEV Community and was authored by Jensen Jose

Recap of Previous Days

  • Day 1: We explored Docker fundamentals, the issues before containers, how Docker solves these problems, the differences between Docker and virtual machines, and an overview of Docker's workflow and architecture.
  • Day 2: We dockerized a sample application, installed Docker Desktop, cloned an application, wrote a Dockerfile, and explored Docker commands like build, tag, push, pull, and run.

Today's Focus: Docker Multi-Stage Builds

Previously, we faced challenges with our Dockerfile, which resulted in an image size of over 200 MB, even with a lightweight Alpine image. Today, we'll use Docker multi-stage builds to significantly reduce that image size.

Docker animated image

Step-by-Step Guide to Multi-Stage Builds

  1. Clone the Application:
git clone https://github.com/docker/getting-started-app.git
cd getting-started-app
  1. Create a Dockerfile:
touch Dockerfile
vi Dockerfile
  1. Write the Dockerfile:
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:latest AS deployer
COPY --from=builder /app/build /usr/share/nginx/html
  1. Build the Docker Image:
docker build -t multi-stage .
  1. Run the Docker Container:
docker run -p 3000:80 multi-stage

Key Benefits of Multi-Stage Builds

  • Reduced Image Size: By only copying necessary files from the builder stage to the final image.
  • Improved Performance: Smaller images mean faster deployment times and reduced resource consumption.
  • Enhanced Security: Only the essential files are included in the final image, reducing the attack surface.

Conclusion

Multi-stage builds are a best practice for creating efficient, secure, and high-performance Docker images. They help in isolating different build stages and only including the necessary artefacts in the final image. This approach not only reduces the image size but also enhances the overall performance and security of the Docker containers.

Thank you for reading, and stay tuned for the next entry in our CKA series. Happy learning!


This content originally appeared on DEV Community and was authored by Jensen Jose


Print Share Comment Cite Upload Translate Updates
APA

Jensen Jose | Sciencx (2024-06-19T15:13:07+00:00) Mastering Multi-Stage Builds in Docker 🚀. Retrieved from https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/

MLA
" » Mastering Multi-Stage Builds in Docker 🚀." Jensen Jose | Sciencx - Wednesday June 19, 2024, https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/
HARVARD
Jensen Jose | Sciencx Wednesday June 19, 2024 » Mastering Multi-Stage Builds in Docker 🚀., viewed ,<https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/>
VANCOUVER
Jensen Jose | Sciencx - » Mastering Multi-Stage Builds in Docker 🚀. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/
CHICAGO
" » Mastering Multi-Stage Builds in Docker 🚀." Jensen Jose | Sciencx - Accessed . https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/
IEEE
" » Mastering Multi-Stage Builds in Docker 🚀." Jensen Jose | Sciencx [Online]. Available: https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» Mastering Multi-Stage Builds in Docker 🚀 | Jensen Jose | Sciencx | https://www.scien.cx/2024/06/19/mastering-multi-stage-builds-in-docker-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.