110.00 c

Description: This is the method to use when you want to use multiple images when using Docker and reduce the final image to a compact size. For example, if you install a 1GB package in the first stage and the output is 10MB, and you don't need to use that 1GB in the final stage when you're only using the output, this is the technique to use.

#100#Infra#110#DevOps_Engineer_Infra#110.00#Docker#110.00 c#Multi_stage_builds

Dockerfile

# First Stage Approximately Node image has 330MB
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build

# Second Stage Approximately Alpine image has 5MB
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html

# The final built image is 5MB + output of first Stage

WORKDIR will create folder if you don't have. But when using multi-stage, it will deleted in next stage.