Bazel Docker 容器使用入门

本页面详细介绍了 Bazel 容器的内容、如何使用 Bazel 容器内的 Bazel 构建 abseil-cpp 项目,以及如何使用装载目录的 Bazel 容器直接从主机构建此项目。

使用目录装载从宿主机构建 Abseil 项目

按照本部分中的说明,您可以使用 Bazel 容器进行构建,并在托管环境中签出的源代码。系统会为您执行的每个构建命令启动一个容器。构建结果会缓存在主机环境中,以便可以在不同构建之间重复使用。

将项目克隆到宿主机中的某个目录。

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

创建一个文件夹,其中包含要在各个 build 之间共享的缓存结果。

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 \
  l.gcr.io/google/bazel:latest \
  --output_user_root=/tmp/build_output \
  build //absl/...

添加 --config=asan|tsan|msan build 标志以相应地选择 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 \
  l.gcr.io/google/bazel:latest \
  --output_user_root=/tmp/build_output \
  build --config={asan | tsan | msan} -- //absl/... -//absl/types:variant_test

从容器内部构建 Abseil 项目

根据本部分中的说明,您可以使用 Bazel 容器和容器内的来源进行构建。通过在开发工作流开始时启动容器并在容器内进行更改,系统会缓存构建结果。

在 Bazel 容器中启动 shell:

docker run --interactive --entrypoint=/bin/bash l.gcr.io/google/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 build 标志以相应地选择 AddressSanitizer (asan)、ThreadSanitizer (tsan) 或 MemorySanitizer (msan),使用排错程序构建项目。

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

探索 Bazel 容器

在 Bazel 容器内启动交互式 shell(如果尚未启动)。

docker run -it --entrypoint=/bin/bash l.gcr.io/google/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: 0.17.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Sep 14 10:39:25 2018 (1536921565)
Build timestamp: 1536921565
Build timestamp as int: 1536921565