Multi-stage builds for static sites
📖 Scenario: You want to create a small, efficient Docker image to serve a static website using nginx. To keep the image size small, you will use a multi-stage build. The first stage will build the static site files, and the second stage will use nginx to serve those files.
🎯 Goal: Build a Dockerfile with two stages: one to prepare the static site files and one to serve them with nginx. The final image should only contain the nginx server and the static files.
📋 What You'll Learn
Create a first stage named
builder using the alpine imageAdd a directory
/app in the builder stage and create an index.html file with exact contentCreate a second stage using the
nginx:alpine imageCopy the
index.html file from the builder stage to the /usr/share/nginx/html directory in the second stageExpose port
80 and set the default command to run nginx💡 Why This Matters
🌍 Real World
Multi-stage builds are used to optimize Docker images by separating build and runtime environments. This is common in deploying static websites and web applications.
💼 Career
Understanding multi-stage builds helps you create efficient container images, a key skill for DevOps engineers and developers working with Docker and cloud deployments.
Progress0 / 4 steps