Junior — Middle
Can you explain the concept of multi-stage builds in Docker and how it is used?
sobes.tech AI
Answer from AI
Multi-stage build in Docker allows creating multiple build stages within a single Dockerfile to reduce the size of the final image and separate compilation and packaging steps.
The idea is that in the initial stages, you use a full image with tools for building the application (e.g., compilers, dependencies), and in the final stages, you copy only the artifacts necessary for running into a minimal image.
This enables:
- Reducing the size of the final image by excluding unnecessary files and tools.
- Simplifying Dockerfile maintenance by combining all stages into one file.
Example:
# Build stage
FROM python:3.10-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
COPY . .
# Final image stage
FROM python:3.10-alpine
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app .
ENV PATH=/root/.local/bin:$PATH
CMD ["python", "app.py"]
Here, the first stage installs dependencies and builds the application, while the second copies only the necessary files and dependencies into a lightweight image.