0
0
Dockerdevops~30 mins

Targeting specific stages in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker Multi-Stage Build: Targeting Specific Stages
📖 Scenario: You are building a Docker image for a simple web application. To keep the image size small, you want to use multi-stage builds. You will create two stages: a builder stage that compiles the app, and a final stage that only contains the compiled app.
🎯 Goal: Build a Dockerfile with two stages named builder and final. Then, target the builder stage to build the image.
📋 What You'll Learn
Create a Dockerfile with a builder stage using the node:18 image
Create a final stage using the nginx:alpine image
Copy the compiled app from builder to final
Build the Docker image targeting the builder stage
💡 Why This Matters
🌍 Real World
Multi-stage builds are used in real projects to reduce image size and separate build dependencies from runtime environments.
💼 Career
Knowing how to target specific stages in Docker builds is important for DevOps roles to optimize container images and deployment pipelines.
Progress0 / 4 steps
1
Create the builder stage
Create a Dockerfile with a builder stage that uses the node:18 image and sets the working directory to /app.
Docker
Need a hint?

Use FROM node:18 AS builder to name the stage builder.

2
Add the final stage
Add a final stage that uses the nginx:alpine image and copies the /app/build directory from the builder stage to /usr/share/nginx/html.
Docker
Need a hint?

Use COPY --from=builder to copy files from the builder stage.

3
Add a build command targeting the builder stage
Write the Docker build command that builds the image named myapp-builder targeting the builder stage.
Docker
Need a hint?

Use docker build --target builder -t myapp-builder . to build only the builder stage.

4
Print the build command output
Run the build command docker build --target builder -t myapp-builder . and print the output showing the build stages.
Docker
Need a hint?

Simulate the build output by printing a success message.