使用 Bazel 目标构建 Docker 个图像
Build Docker Images with Bazel targets Built
我有一个 (1) Docker 文件和一个 (2) Bazel 的 C++ 项目
我想创建一个 docker 图像,在图像中预先构建 Bazel 目标,以便在我启动新容器时,Bazel 目标是预先构建的,我只需要 Bazel run //hello:hello_world
来自容器 bash.
Docker文件
# Copy my project with Bazel files to a Docker image, and the
...
RUN bazel --output_user_root=/tmp/hello_project/bazel build //...
...
在 Docker 文件的执行过程中,我得到了预期的以下输出
Loading:
Loading: 0 packages loaded
Analyzing: 2 targets (1 packages loaded, 0 targets configured)
Analyzing: 2 targets (11 packages loaded, 18 targets configured)
INFO: Analyzed 2 targets (15 packages loaded, 60 targets configured).
INFO: Found 2 targets...
[0 / 11] [Prepa] BazelWorkspaceStatusAction stable-status.txt
INFO: Elapsed time: 6.333s, Critical Path: 0.37s
INFO: 11 processes: 6 internal, 5 processwrapper-sandbox.
INFO: Build completed successfully, 11 total actions
INFO: Build completed successfully, 11 total actions
当我 运行 一个新的容器形成之前构建的 Docker 图像,然后在容器上 运行
bazel run //hello:hello_world
它不使用现有的预构建目标,而是重新构建目标,这不是必需的。
我期望的结果(未得到):一切都是预先构建的,只需要 运行
INFO: Analyzed target //hello:hello_world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //hello:hello_world up-to-date:
bazel-bin/hello/hello_world
INFO: Elapsed time: 0.163s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Hello World!
我得到的结果:我重新构建了二进制文件
[root@4a6bdb57fd79 test-rc]# bazel run //hello:hello_world
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
INFO: Analyzed target //hello:hello_world (15 packages loaded, 60 targets configured).
INFO: Found 1 target...
Target //hello:hello_world up-to-date:
bazel-bin/hello/hello_world
INFO: Elapsed time: 6.255s, Critical Path: 0.38s
INFO: 7 processes: 4 internal, 3 processwrapper-sandbox.
INFO: Build completed successfully, 7 total actions
INFO: Build completed successfully, 7 total actions
Hello World!
我如何确保 bazel run
使用相同的预构建目标而不是在 运行 之前再次构建它们。
这听起来像是不确定性 - 在 Bazel 的第二次执行中,有一些不同导致缓存未命中。
一些可能导致它的原因:
- 传递给 bazel 构建与 bazel 测试的不同选项 - 检查您的
.bazelrc
文件
- 包含 VCS 信息或时间戳的操作 - 确保你有 rules_docker 0.22.0 或更高版本来获取 https://github.com/bazelbuild/rules_docker/commit/2b35b2dd56f0be6cc6b8df957332a31435f6b3ce
诊断的一个步骤是对 Bazel 使用 --explain=log.txt
和 --verbose_explanations
标志,然后日志文件会说明重建的原因。但是它没有太多细节,只是类似“源文件已更改”之类的内容。
如果你想要这个强大的工具,有一种方法可以找出 Bazel 没有获得缓存命中的确切原因 - 阅读 https://georgi.hristozov.net/til/2020/04/20/compare-bazel-execlogs-to-find-non-deterministic-parts-of-the-build
我有一个 (1) Docker 文件和一个 (2) Bazel 的 C++ 项目
我想创建一个 docker 图像,在图像中预先构建 Bazel 目标,以便在我启动新容器时,Bazel 目标是预先构建的,我只需要 Bazel run //hello:hello_world
来自容器 bash.
Docker文件
# Copy my project with Bazel files to a Docker image, and the
...
RUN bazel --output_user_root=/tmp/hello_project/bazel build //...
...
在 Docker 文件的执行过程中,我得到了预期的以下输出
Loading:
Loading: 0 packages loaded
Analyzing: 2 targets (1 packages loaded, 0 targets configured)
Analyzing: 2 targets (11 packages loaded, 18 targets configured)
INFO: Analyzed 2 targets (15 packages loaded, 60 targets configured).
INFO: Found 2 targets...
[0 / 11] [Prepa] BazelWorkspaceStatusAction stable-status.txt
INFO: Elapsed time: 6.333s, Critical Path: 0.37s
INFO: 11 processes: 6 internal, 5 processwrapper-sandbox.
INFO: Build completed successfully, 11 total actions
INFO: Build completed successfully, 11 total actions
当我 运行 一个新的容器形成之前构建的 Docker 图像,然后在容器上 运行
bazel run //hello:hello_world
它不使用现有的预构建目标,而是重新构建目标,这不是必需的。
我期望的结果(未得到):一切都是预先构建的,只需要 运行
INFO: Analyzed target //hello:hello_world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //hello:hello_world up-to-date:
bazel-bin/hello/hello_world
INFO: Elapsed time: 0.163s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Hello World!
我得到的结果:我重新构建了二进制文件
[root@4a6bdb57fd79 test-rc]# bazel run //hello:hello_world
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
INFO: Analyzed target //hello:hello_world (15 packages loaded, 60 targets configured).
INFO: Found 1 target...
Target //hello:hello_world up-to-date:
bazel-bin/hello/hello_world
INFO: Elapsed time: 6.255s, Critical Path: 0.38s
INFO: 7 processes: 4 internal, 3 processwrapper-sandbox.
INFO: Build completed successfully, 7 total actions
INFO: Build completed successfully, 7 total actions
Hello World!
我如何确保 bazel run
使用相同的预构建目标而不是在 运行 之前再次构建它们。
这听起来像是不确定性 - 在 Bazel 的第二次执行中,有一些不同导致缓存未命中。
一些可能导致它的原因:
- 传递给 bazel 构建与 bazel 测试的不同选项 - 检查您的
.bazelrc
文件 - 包含 VCS 信息或时间戳的操作 - 确保你有 rules_docker 0.22.0 或更高版本来获取 https://github.com/bazelbuild/rules_docker/commit/2b35b2dd56f0be6cc6b8df957332a31435f6b3ce
诊断的一个步骤是对 Bazel 使用 --explain=log.txt
和 --verbose_explanations
标志,然后日志文件会说明重建的原因。但是它没有太多细节,只是类似“源文件已更改”之类的内容。
如果你想要这个强大的工具,有一种方法可以找出 Bazel 没有获得缓存命中的确切原因 - 阅读 https://georgi.hristozov.net/til/2020/04/20/compare-bazel-execlogs-to-find-non-deterministic-parts-of-the-build