Junior — Middle
What is the purpose of using multiple FROM instructions when creating a Docker image?
sobes.tech AI
Answer from AI
Using multiple FROM instructions in a Dockerfile allows for multi-stage builds. This helps reduce the size of the final image and separates the build process into logical stages.
Main advantages:
- Different base images can be used for different stages in a single Dockerfile (e.g., an image with a compiler for building and a minimal image for running).
- In the final stage, only the necessary artifacts can be copied from previous stages, excluding build tools and temporary files.
Example:
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]