0

Can anyone explain the advantages of multi-stage builds, especially what is going on here in this specific Dockerfile example?

ref: Section titled: Create an image from an alternative base image


Question:

What advantage does this approach have:

FROM python:buster as build-image
ARG FUNCTION_DIR="/function"

<instructions>

FROM python:buster
# Copy in the build image dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
<more instructions>

over this approach

FROM python:buster
<all needed instructions>

I'm not seeing the advantage, or why this approach would be taken but I do not doubt there is some advantage to this approach.


copy of Dockerfile from link above

# Define function directory
ARG FUNCTION_DIR="/function"

FROM python:buster as build-image

# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
  apt-get install -y \
  g++ \
  make \
  cmake \
  unzip \
  libcurl4-openssl-dev

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}

# Copy function code
COPY app/* ${FUNCTION_DIR}

# Install the runtime interface client
RUN pip install \
        --target ${FUNCTION_DIR} \
        awslambdaric

# Multi-stage build: grab a fresh copy of the base image
FROM python:buster

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the build image dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]

1 Answer 1

1

The size of the final image ;-)

The first stage installs required binaries and libs to compile something and the next stage just copies the compiled stuff to an "empty" image.

You dont need to ship the whole build environment to run the software...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .