開始使用 Bazel Docker 容器

本頁詳細說明 Bazel 容器的內容、如何使用 Bazel 容器內的 Bazel 建構 abseil-cpp 專案,以及如何使用 Bazel 容器 (含目錄掛接) 直接從主機建構這個專案。

透過目錄掛接,從主機建構 Abseil 專案

本節的操作說明可協助您使用 Bazel 容器建構,並在主機環境中簽出來源。您執行的每個建構指令都會啟動容器。建構結果會快取在主機環境中,因此可在建構作業中重複使用。

將專案複製到主機的目錄。

git clone https://github.com/abseil/abseil-cpp.git /src/workspace

建立資料夾,其中包含可跨建構分享的快取結果。

mkdir -p /tmp/build_output/

使用 Bazel 容器建構專案,並在主機的輸出資料夾中提供建構輸出內容。

docker run \
  -e USER="$(id -u)" \
  -u="$(id -u)" \
  -v /src/workspace:/src/workspace \
  -v /tmp/build_output:/tmp/build_output \
  -w /src/workspace \
  gcr.io/bazel-public/bazel:latest \
  --output_user_root=/tmp/build_output \
  build //absl/...

如要使用清除器建構專案,請新增 --config=asan|tsan|msan 建構標記,然後視需要選取 AddressSanitizer (asan)、ThreadSanitizer (tsan) 或 MemorySanitizer (msan)。

docker run \
  -e USER="$(id -u)" \
  -u="$(id -u)" \
  -v /src/workspace:/src/workspace \
  -v /tmp/build_output:/tmp/build_output \
  -w /src/workspace \
  gcr.io/bazel-public/bazel:latest \
  --output_user_root=/tmp/build_output \
  build --config={asan | tsan | msan} -- //absl/... -//absl/types:variant_test

從容器內建構 Abseil 專案

本節的操作說明可讓您使用 Bazel 容器建構,來源位於容器內。在開發工作流程開始時啟動容器,並在容器內的工作區中進行變更,即可快取建構結果。

在 Bazel 容器中啟動殼層:

docker run --interactive --entrypoint=/bin/bash gcr.io/bazel-public/bazel:latest

每個容器 ID 都是不重複的。在下列操作說明中,容器為 5a99103747c6。

複製專案。

root@5a99103747c6:~# git clone https://github.com/abseil/abseil-cpp.git && cd abseil-cpp/

定期建構。

root@5a99103747c6:~/abseil-cpp# bazel build //absl/...

如要使用清除器建構專案,請新增建構標記 --config=asan|tsan|msan,然後視需要選取 AddressSanitizer (asan)、ThreadSanitizer (tsan) 或 MemorySanitizer (msan)。

root@5a99103747c6:~/abseil-cpp# bazel build --config=--config={asan | tsan | msan} -- //absl/... -//absl/types:variant_test

探索 Bazel 容器

如果尚未啟動,請在 Bazel 容器中啟動互動式殼層。

docker run -it --entrypoint=/bin/bash gcr.io/bazel-public/bazel:latest
root@5a99103747c6:/#

探索容器內容。

root@5a99103747c6:/# clang --version
clang version 8.0.0 (trunk 340178)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
root@5a99103747c6:/# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
root@5a99103747c6:/# python -V
Python 2.7.12
root@5a99103747c6:/# python3 -V
Python 3.6.6
root@5a99103747c6:/# bazel version
Extracting Bazel installation...
Build label: 6.0.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Dec 19 15:52:35 2022 (1671465155)
Build timestamp: 1671465155
Build timestamp as int: 1671465155