Junior — Middle
How have you worked with multi-stage container builds in Dockerfile?
sobes.tech AI
Answer from AI
Multi-stage build in Dockerfile allows creating multiple intermediate images within a single Dockerfile to reduce the final image size and separate build stages from the runtime stage.
For example, one stage builds the application with necessary tools and dependencies, and then only the required artifacts are copied into a minimal base image in the second stage.
Example Dockerfile with multi-stage build for a Go application:
# Build stage
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Runtime stage
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]
This approach prevents including source code and build tools in the final image, reducing size and increasing security.