The Dockerfile: Two Stages
Think of this like a kitchen: the first stage is the messy prep area where you chop vegetables (the builder), and the second stage is the clean plate you serve to the customer (the production image).
Stage 1: The Builder
Dockerfile
FROM ubuntu:latest as builder
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html
FROM ubuntu:latest as builder: Starts with a full Ubuntu OS. We name this stage builder so we can reference it later.
RUN …: Updates the system and installs Nginx. This is a “heavy” step because it downloads many tools we don’t need for the final run.
COPY index.html …: Moves your website file from your computer into this temporary container.
Stage 2: The Production Image
Dockerfile
FROM nginx:alpine
COPY --from=builder /var/www/html /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;" ]
FROM nginx:alpine: Switches to a tiny, security-hardened version of Linux (Alpine).
COPY –from=builder: This is the magic. It reaches back into the builder stage, grabs only the index.html file, and brings it here. All the Ubuntu “bulk” is left behind.
CMD: Tells the container to start Nginx and keep it running in the foreground.