有人能解释一下多阶段构建的优点吗,特别是在这个特定Dockerfile例子?
参考:标题为:的章节 从备用基础映像创建映像
问题:
这种方法有什么优点:
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>
通过这种方法
FROM python:buster
<all needed instructions>
我没有看到这种方法的优势,或者为什么要采用这种方法,但我并不怀疑这种方法确实有优势。
从上面的链接复制 Dockerfile
# 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
最终图像的大小;-)
第一阶段安装编译某些内容所需的二进制文件和库,下一阶段仅将编译的内容复制到“空”图像中。
您不需要运送整个构建环境来运行该软件......