--- title: 【軟體】利用 Multi-Stage Builds 節省容量 tags: TTennis Pickup Robot disqus: hackmd --- <h1 style="text-align: center; color: orange;"> 🛠️ 【軟體】必學的 Docker 🛠️ </h1> <h2 style="text-align: center; color: skyblue;">利用 Multi-Stage Builds 節省容量</h2> <center> 蠻多安裝依賴項目,官方會提供兩種安裝方式, 「Install binary files」或「Build from source」。 選擇「Build from source」代表要自己下載源文件並在本地編譯。 但在過程中的編譯環境、工具,在之後的部署階段可能是不需要的。 這時就是 Multi-Stage Build 的登場時機。 :::info Multi-Stage Builds 理念是使用多個 stage 來構建 Image。 並拷貝前面階段編譯的二進制文件至後續階段。 ::: 即便毋須降低容量,用 Multi-Stage Build 的概念來寫也會比較整潔。 </center> </br> <h3><font color ="magenza"> 1. 實際範例</font></h3> :::success 整段示範代碼的邏輯: 1. 先以 **tools 階段**為基礎新增 **depthai-builder 階段**,去處理編譯問題。 2. 再同樣以 **tools** 為基礎構建 **final 階段**,並 copy **depthai-build** 的二進制文件。 ::: * **多階段構建 depthai-core** * `FROM A AS B`:從 Image A 或階段 A 為基礎,開始構建階段 B。 ```dockerfile= ################################################################################################ # - DepthAI Core Build Stage # - In this stage, we will build DepthAI Core and extract necessary binary files. ################################################################################################ FROM tools AS depthai-builder ## 安裝只有編譯期間才需要的工具 RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y \ cmake \ libclang-dev \ libssl-dev \ patchelf \ && rm -rf /var/lib/apt/lists/* ## 這裡下載源代碼並進行編譯 RUN git clone https://github.com/luxonis/depthai-core.git && \ cd depthai-core && \ git submodule update --init --recursive && \ mkdir -p build && cd build && \ cmake .. && \ cmake --build . --parallel $(nproc) && \ cmake --install . --prefix /usr/local && \ cd .. && cmake -S. -Bbuild -D'DEPTHAI_BUILD_EXAMPLES=ON' && \ cmake --build build ################################################################################################ # - Final Stage # - In this stage, we will install necessary packages and copy DepthAI Core binary files. ################################################################################################ FROM tools AS final ## 只 copy 編譯後的二進制文件 COPY --from=depthai-builder /usr/local/lib/libdepthai-core.so* /usr/local/lib/ COPY --from=depthai-builder /usr/local/include/depthai /usr/local/include/depthai COPY --from=depthai-builder /usr/local/lib/cmake/depthai /usr/local/lib/cmake/depthai # ... ``` * **平常我就習慣用多階段構建的概念寫 Dockerfile** * 參考範例 RTAB-Map ```dockerfile= ################################################################################################ # - Base stage # - This stage serves as the foundational stage for all other stages. # - Base image: OSRF ROS Humble Desktop Full # - https://hub.docker.com/r/osrf/ros/tags?page=1&name=humble-desktop-full ################################################################################################ FROM osrf/ros:humble-desktop-full AS base LABEL org.opencontainers.image.authors="yoseph.huang@gmail.com" ARG USERNAME=user ARG USER_UID=1000 ARG USER_GID=$USER_UID SHELL ["/bin/bash", "-c"] ################################################################################################ # - User Setup stage # - In this stage, I will create a non-root user and configure passwordless sudo. ################################################################################################ FROM base AS user-setup ## Create a non-root user RUN groupadd --gid $USER_GID $USERNAME && \ useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash && \ apt-get update && \ apt-get install -y sudo && \ echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME && \ chmod 0440 /etc/sudoers.d/$USERNAME && \ rm -rf /var/lib/apt/lists/* ################################################################################################ # - Tools Installation stage # - In this stage, I will install convenient tools for development. ################################################################################################ FROM user-setup AS tools ## Install necessary packages RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y \ apt-utils \ git \ nano \ net-tools \ ssh \ usbutils \ udev \ x11-apps \ && rm -rf /var/lib/apt/lists/* ################################################################################################ # - Final stage # - In this stage, I will install required packages for RTAB-Map and Intel RealSense. # - Reference: # - https://github.com/introlab/rtabmap_ros/tree/humble-devel?tab=readme-ov-file#installation # - https://github.com/introlab/rtabmap_ros.git ################################################################################################ FROM tools AS final ## Install Intel RealSense dependencies RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y \ ros-humble-librealsense2* \ ros-humble-realsense2-* && \ rm -rf /var/lib/apt/lists/* ## Install RTAB-Map dependencies RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y \ ros-humble-rtabmap-ros && \ rm -rf /var/lib/apt/lists/* ## Install foxglove-bridge RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y \ ros-humble-foxglove-bridge && \ rm -rf /var/lib/apt/lists/* ## Set working directory RUN echo "source /opt/ros/$ROS_DISTRO/setup.bash" >> ~/.bashrc WORKDIR /home/slam-ws ## Final configurations # USER $USERNAME CMD ["/bin/bash"] ```