Sobes.tech
Junior — Middle

Can multiple FROM instructions be used within a single Dockerfile?

sobes.tech AI

Answer from AI

Yes, it is possible to use multiple FROM instructions in a single Dockerfile. This is called multi-stage build. Each FROM instruction starts a new build stage, allowing you to split the image creation process into multiple steps. For example, you can first build the application in one image, and then copy only the necessary artifacts into the final image, reducing its size.

Example:

FROM python:3.9-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

FROM python:3.9-alpine
WORKDIR /app
COPY --from=builder /app /app
CMD ["python", "app.py"]

Here, the first stage builds dependencies, and the second creates a lightweight final image.