蠻多安裝依賴項目,官方會提供兩種安裝方式,
「Install binary files」或「Build from source」。
選擇「Build from source」代表要自己下載源文件並在本地編譯。
但在過程中的編譯環境、工具,在之後的部署階段可能是不需要的。
這時就是 Multi-Stage Build 的登場時機。
Multi-Stage Builds 理念是使用多個 stage 來構建 Image。
並拷貝前面階段編譯的二進制文件至後續階段。
即便毋須降低容量,用 Multi-Stage Build 的概念來寫也會比較整潔。
整段示範代碼的邏輯:
多階段構建 depthai-core
FROM A AS B
:從 Image A 或階段 A 為基礎,開始構建階段 B。
################################################################################################
# - 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
################################################################################################
# - 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"]
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up