Junior — Middle
What is the purpose of using multi-stage builds in Docker and what are their advantages?
sobes.tech AI
Answer from AI
Multi-stage builds in Docker allow creating images in multiple stages, where the first stages involve building and compiling the application, and the final stage forms a minimal image containing only the artifacts necessary for running.
Advantages:
- Significant reduction in the size of the final image, as build tools and temporary files are not included.
- Increased security by excluding unnecessary components from the image.
- Simplification of the build process and Dockerfile maintenance, as all stages are described in a single file.
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"]