Copying from build stage to final stage
📖 Scenario: You are creating a Docker image for a simple web application. To keep the final image small, you want to use a multi-stage build. The first stage will build the application, and the second stage will copy only the necessary files from the build stage.
🎯 Goal: Build a multi-stage Dockerfile where the final stage copies the built application files from the build stage.
📋 What You'll Learn
Create a build stage named
builder using the node:18 imageIn the build stage, create a directory
/app and add a file index.js with the content console.log('Hello from build stage');Create a final stage using the
node:18-slim imageCopy the
index.js file from the builder stage to the final stage's /app directorySet the working directory to
/app in the final stageSet the default command to run
node index.js💡 Why This Matters
🌍 Real World
Multi-stage builds are used in real projects to separate building and running environments. This keeps the final Docker image small and secure by excluding build tools.
💼 Career
Understanding multi-stage Docker builds is important for DevOps roles, as it improves deployment efficiency and image management.
Progress0 / 4 steps