0
0
Dockerdevops~30 mins

Multiple FROM statements in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the first base image with Node.js
Write the first FROM statement using the exact image node:18. Then create a working directory called /app.
Docker
Need a hint?

Start your Dockerfile with FROM node:18 and then add WORKDIR /app on the next line.

2
Add build commands and second base image
Add commands to copy package.json and install dependencies using npm install. Then add a second FROM statement using the exact image nginx:alpine.
Docker
Need a hint?

Use COPY package.json . to copy the file and RUN npm install to install dependencies. Then start the second stage with FROM nginx:alpine.

3
Copy built files and expose port
Copy the built app files from the first stage to /usr/share/nginx/html in the second stage using COPY --from=0 /app/build /usr/share/nginx/html. Then expose port 80.
Docker
Need a hint?

Use COPY --from=0 to copy files from the first stage. Then add EXPOSE 80 to open the port.

4
Print the final Dockerfile content
Print the entire Dockerfile content as a single string exactly as written in previous steps.
Docker
Need a hint?

Use a triple-quoted string to store the Dockerfile content and print it.