Using Multiple FROM Statements in Dockerfiles
📖 Scenario: You are creating a Docker image for a simple web application. You want to use one base image to build the application and another smaller image to run it. This helps keep the final image small and efficient.
🎯 Goal: Build a Dockerfile that uses two FROM statements: one to build the app and one to run the app. You will copy the built files from the first image to the second image.
📋 What You'll Learn
Create a Dockerfile with two
FROM statements.Use
node:18 as the first base image to build the app.Use
nginx:alpine as the second base image to serve the app.Copy the built app files from the first image to the second image.
Expose port 80 in the final image.
💡 Why This Matters
🌍 Real World
Multi-stage Dockerfiles are used to optimize container images by separating build and runtime environments. This reduces image size and improves security.
💼 Career
Understanding multi-stage builds is important for DevOps roles that involve containerization and deployment pipelines.
Progress0 / 4 steps