Middle+
What mechanism in Docker is used to reduce the size of the final image?
sobes.tech AI
Answer from AI
To reduce the size of the final Docker image, a multi-stage build mechanism is used. It allows dividing the build process into several stages, where in the initial stages the application is built and compiled, and in the final stage, only the files necessary for running are copied into the final image, without build tools and temporary data.
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
# Final image
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]
This approach significantly reduces the size of the final image, as it lacks source files, compilers, and temporary files.