Docker Multi-Stage Build for a Simple Web App
📖 Scenario: You want to create a small web application using Docker. To keep your final Docker image small and efficient, you will use a multi-stage build. This means you will first build your app in one stage, then copy only the necessary files to a smaller image in the final stage.
🎯 Goal: Build a Dockerfile using multi-stage builds that compiles a simple static website in the first stage and then copies the built files to a lightweight web server image in the second stage.
📋 What You'll Learn
Create a first build stage named
builder using the node:18-alpine imageIn the builder stage, create a directory
/app and add a file index.html with the exact content <h1>Hello from Builder</h1>Create a second stage using the
nginx:alpine imageCopy the
index.html file from the builder stage to /usr/share/nginx/html/index.html in the final imageSet the default command to run
nginx in the foreground💡 Why This Matters
🌍 Real World
Multi-stage builds are used to create small, secure, and efficient Docker images by separating build and runtime environments.
💼 Career
Understanding multi-stage builds is essential for DevOps roles to optimize container images and improve deployment speed.
Progress0 / 4 steps