Middle
How can you reduce the size of a Docker image?
sobes.tech AI
Answer from AI
To reduce the size of a Docker image, you can use several approaches:
- Choosing a base image: Use minimalist images, such as
alpineinstead ofubuntu. - Multi-stage builds: Build the application in one image, then copy only the necessary artifacts into the final image.
- Optimizing layers: Combine RUN commands to reduce the number of layers.
- Removing temporary files: Delete caches, temporary files, and packages after installation.
- Minimizing dependencies: Install only the necessary packages and libraries.
Example of a multi-stage build:
FROM golang:1.20-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine
COPY --from=builder /app/myapp /myapp
CMD ["/myapp"]