blob: 702a32204b66b707bf842083d32aa4bb3a8927dc [file] [log] [blame] [view]
xingaoecbf6512018-10-02 07:28:15 -07001---
2layout: documentation
Googler817e2202020-11-20 19:14:47 -08003title: Bazel container
xingaoecbf6512018-10-02 07:28:15 -07004---
5
Googler817e2202020-11-20 19:14:47 -08006# Getting Started with Bazel Docker Container
xingaoecbf6512018-10-02 07:28:15 -07007
Googlercb395cb2020-12-21 10:33:20 -08008This page provides details on the contents of the Bazel container, how to build
9the [abseil-cpp](https://github.com/abseil/abseil-cpp) project using Bazel
10inside the Bazel container, and how to build this project directly
11from the host machine using the Bazel container with directory mounting.
xingaoecbf6512018-10-02 07:28:15 -070012
13## Build Abseil project from your host machine with directory mounting
14
15The instructions in this section allow you to build using the Bazel container
16with the sources checked out in your host environment. A container is started up
17for each build command you execute. Build results are cached in your host
18environment so they can be reused across builds.
19
20Clone the project to a directory in your host machine.
21
22```bash
23git clone https://github.com/abseil/abseil-cpp.git /src/workspace
24```
25
26Create a folder that will have cached results to be shared across builds.
27
28```bash
29mkdir -p /tmp/build_output/
30```
31
32Use the Bazel container to build the project and make the build
33outputs available in the output folder in your host machine.
34
35```bash
36docker run \
37 -e USER="$(id -u)" \
38 -u="$(id -u)" \
39 -v /src/workspace:/src/workspace \
40 -v /tmp/build_output:/tmp/build_output \
41 -w /src/workspace \
Rgis Dcamps223ef9d2019-12-21 17:15:59 -080042 l.gcr.io/google/bazel:latest \
xingaoecbf6512018-10-02 07:28:15 -070043 --output_user_root=/tmp/build_output \
44 build //absl/...
45```
46
47Build the project with sanitizers by adding the --config=<asan/tsan/msan> build
48flag to select AddressSanitizer (asan), ThreadSanitizer (tsan) or
49MemorySanitizer (msan) accordingly.
50
51```bash
52docker run \
53 -e USER="$(id -u)" \
54 -u="$(id -u)" \
55 -v /src/workspace:/src/workspace \
56 -v /tmp/build_output:/tmp/build_output \
57 -w /src/workspace \
Rgis Dcamps223ef9d2019-12-21 17:15:59 -080058 l.gcr.io/google/bazel:latest \
xingaoecbf6512018-10-02 07:28:15 -070059 --output_user_root=/tmp/build_output \
60 build --config=<asan/tsan/msan> -- //absl/... -//absl/types:variant_test
61```
62
63## Build Abseil project from inside the container
64
65The instructions in this section allow you to build using the Bazel container
Rgis Dcamps223ef9d2019-12-21 17:15:59 -080066with the sources inside the container. By starting a container at the beginning
67of your developement workflow and doing changes in the worskpace within the
68container, build results will be cached.
69
70Start a shell in the Bazel container:
71
72```bash
73docker run --interactive --entrypoint=/bin/bash l.gcr.io/google/bazel:latest
74```
75
76Each container id is unique. In the instructions bellow, the container was 5a99103747c6.
xingaoecbf6512018-10-02 07:28:15 -070077
78Clone the project.
79
80```bash
81root@5a99103747c6:~# git clone https://github.com/abseil/abseil-cpp.git && cd abseil-cpp/
82```
83
84Do a regular build.
85
86```bash
87root@5a99103747c6:~/abseil-cpp# bazel build //absl/...
88```
89
Rgis Dcamps223ef9d2019-12-21 17:15:59 -080090Build the project with sanitizers by adding the `--config=<asan/tsan/msan>` build
xingaoecbf6512018-10-02 07:28:15 -070091flag to select AddressSanitizer (asan), ThreadSanitizer (tsan) or
92MemorySanitizer (msan) accordingly.
93
94```bash
95root@5a99103747c6:~/abseil-cpp# bazel build --config=<asan/tsan/msan> -- //absl/... -//absl/types:variant_test
96```
97
98## Explore the Bazel container
99
Rgis Dcamps223ef9d2019-12-21 17:15:59 -0800100If you haven't already, start an interactive shell inside the Bazel container.
xingaoecbf6512018-10-02 07:28:15 -0700101
102```bash
Rgis Dcamps223ef9d2019-12-21 17:15:59 -0800103docker run -it --entrypoint=/bin/bash l.gcr.io/google/bazel:latest
xingaoecbf6512018-10-02 07:28:15 -0700104root@5a99103747c6:/#
105```
106
107Explore the container contents.
108
109```bash
110root@5a99103747c6:/# clang --version
111clang version 8.0.0 (trunk 340178)
112Target: x86_64-unknown-linux-gnu
113Thread model: posix
114InstalledDir: /usr/local/bin
115
116root@5a99103747c6:/# java -version
117openjdk version "1.8.0_181"
118OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13)
119OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
120
121root@5a99103747c6:/# python -V
122Python 2.7.12
123
124root@5a99103747c6:/# python3 -V
125Python 3.6.6
126
127root@5a99103747c6:/# bazel version
128Extracting Bazel installation...
129Build label: 0.17.1
130Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
131Build time: Fri Sep 14 10:39:25 2018 (1536921565)
132Build timestamp: 1536921565
133Build timestamp as int: 1536921565
134```