Sobes.tech
Junior — Middle

Have you used multi-stage Docker files in your projects?

sobes.tech AI

Answer from AI

Yes, I have used multi-stage Dockerfiles in projects to optimize image size and separate build and runtime stages.

Example of a multi-stage Dockerfile for a Python application:

# Build stage
FROM python:3.9-slim AS builder
WORKDIR /app
COPY requirements.txt ./
RUN pip install --user -r requirements.txt
COPY . ./

# Runtime stage
FROM python:3.9-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app /app
ENV PATH=/root/.local/bin:$PATH
CMD ["python", "app.py"]

This approach allows you to first build all dependencies and the application, then transfer only the necessary artifacts to the final image, reducing its size and increasing security.