“COPY --from=build-env /app/build/web /usr/share/nginx/html” 起什么作用?

“COPY --from=build-env /app/build/web /usr/share/nginx/html” 起什么作用?

我有Dockerfile以下内容:

# Install Operating system and dependencies
FROM ubuntu:22.04 AS build-env

RUN apt-get update 
RUN apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3
RUN apt-get clean

RUN useradd -ms /bin/bash user
USER user
WORKDIR /home/user

# download Flutter SDK from Flutter Github repo
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter

# Set flutter environment path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"

# Run flutter doctor
RUN flutter doctor

# Enable flutter web
RUN flutter channel master
RUN flutter upgrade
RUN flutter config --enable-web

# Copy files to container and build
RUN mkdir /app/
COPY . /app/
WORKDIR /app/
RUN flutter build web


# Stage 2 - Create the run-time image
FROM nginx:1.21.1-alpine
COPY --from=build-env /app/build/web /usr/share/nginx/html

我想知道激光部分(第 2 阶段)起什么作用?

答案1

这是一种常见的模式,其中您有一个包含工具、SDK 等的构建器映像,然后是一个没有开发依赖项的最小运行时映像。

在本例中,第一个镜像用于实际构建 Flutter 应用程序(您可以看到它通过 apt-get 添加了一些操作系统依赖项,然后克隆了 Flutter SDK)。然后它构建一个 Web 应用程序(“--enable-web”)并将其输出到“/app/build/web”中。

现在 Web 构件已经构建完毕,您只需要一个包含 Web 服务器(本例中为“nginx”)和构建构件的最小映像。因此,第 2 阶段正是这样做的:最小 nginx 容器加上第 1 阶段构建的 Web 构件。

相关内容