|
| 1 | +--- |
| 2 | +title: Build in Docker |
| 3 | +description: Building RIOT in a Docker container |
| 4 | +--- |
| 5 | + |
| 6 | +Some parts of RIOT's build process can be performed inside a Docker container, |
| 7 | +which comes with the necessary compilers and toolchains and is fully managed by |
| 8 | +the build system. It can be enabled by passing `BUILD_IN_DOCKER=1` to make. |
| 9 | + |
| 10 | +```shell |
| 11 | +$ BUILD_IN_DOCKER=1 make |
| 12 | +``` |
| 13 | + |
| 14 | +If your user does not have permissions to access the Docker daemon: |
| 15 | + |
| 16 | +```shell |
| 17 | +$ BUILD_IN_DOCKER=1 DOCKER="sudo docker" make |
| 18 | +``` |
| 19 | + |
| 20 | +to always use Docker for building, set `BUILD_IN_DOCKER=1` (and if necessary |
| 21 | +`DOCKER="sudo docker"`) in the environment: |
| 22 | + |
| 23 | +```console |
| 24 | +$ export BUILD_IN_DOCKER=1 |
| 25 | +``` |
| 26 | + |
| 27 | +## Targets ran in Docker: DOCKER_MAKECMDGOALS_POSSIBLE |
| 28 | + |
| 29 | +Currently only build related targets are ran in the docker container, the exact |
| 30 | +list is under `DOCKER_MAKECMDGOALS_POSSIBLE` variable. |
| 31 | + |
| 32 | +## Environment Variables: DOCKER_ENV_VARS |
| 33 | + |
| 34 | +When building in docker one might want for the command ran in docker to inherit |
| 35 | +variables that might have been set in the command line. e.g.: |
| 36 | + |
| 37 | +```shell |
| 38 | +BOARD=samr21-xpro USEMODULE=xtimer make -C examples/hello-world |
| 39 | +``` |
| 40 | + |
| 41 | +In `docker.ink.mk` the origin of a variable listed in `DOCKER_ENV_VARS` is checked |
| 42 | +and if the origin is `environment` or `command` (for make command-line argument) |
| 43 | +then those variables will be passed to the docker container. |
| 44 | + |
| 45 | +You can also set in `DOCKER_ENV_VARS` in the environment to add variables to the |
| 46 | +list, e.g.: |
| 47 | + |
| 48 | +```shell |
| 49 | +DOCKER_ENV_VARS=BEER_TYPE BEER_TYPE="imperial stout" BUILD_IN_DOCKER=1 make -C examples/hello-world/ |
| 50 | +docker run --rm -t -u "$(id -u)" \ |
| 51 | + ... |
| 52 | + -e 'BEER_TYPE=imperial stout' \ |
| 53 | + -w '/data/riotbuild/riotbase/examples/hello-world/' \ |
| 54 | + 'riot/riotbuild:latest' make |
| 55 | +``` |
| 56 | + |
| 57 | +Your application Makefile can also extend `DOCKER_ENV_VARS`. |
| 58 | + |
| 59 | +### Directly Define Environment Variables: DOCKER_ENVIRONMENT_CMDLINE |
| 60 | + |
| 61 | +`DOCKER_ENVIRONMENT_CMDLINE` can be used to add variables directly to the environment |
| 62 | +but will need to be prefixed with `-e` (see [option-summary]). |
| 63 | + |
| 64 | +e.g.: |
| 65 | + |
| 66 | +``` |
| 67 | +DOCKER_ENVIRONMENT_CMDLINE='-e BEER_TYPE="imperial stout"' BUILD_IN_DOCKER=1 make -C examples/hello-world/ |
| 68 | +docker run --rm -t -u "$(id -u)" \ |
| 69 | + ... |
| 70 | + -e 'BEER_TYPE=imperial stout' \ |
| 71 | + -w '/data/riotbuild/riotbase/examples/hello-world/' \ |
| 72 | + 'riot/riotbuild:latest' make |
| 73 | +``` |
| 74 | + |
| 75 | +## Command Line Variables: DOCKER_OVERRIDE_CMDLINE |
| 76 | + |
| 77 | +Command line arguments are variables that are passed after `make` e.g. |
| 78 | +`make target FOO=bar`, but different to environment variables a variable defined |
| 79 | +through the command line will take precedence over all assignments of `FOO` within |
| 80 | +the makefile (same effect as adding `-e` for environment variables, see |
| 81 | +[option-summary] for more details. |
| 82 | + |
| 83 | +To pass variables overriding the command-line to docker `DOCKER_OVERRIDE_CMDLINE` |
| 84 | +may be used: |
| 85 | + |
| 86 | +```shell |
| 87 | +DOCKER_OVERRIDE_CMDLINE="BEER_TYPE='imperial stout'" BUILD_IN_DOCKER=1 make -C examples/hello-world/ RIOT_CI_BUILD=1 |
| 88 | +Launching build container using image "riot/riotbuild:latest". |
| 89 | +sudo docker run --rm -t -u "$(id -u)" \ |
| 90 | + ... |
| 91 | + -w '/data/riotbuild/riotbase/examples/hello-world/' \ |
| 92 | + 'riot/riotbuild:latest' make BEER_TYPE='imperial stout' 'RIOT_CI_BUILD=1' |
| 93 | +``` |
| 94 | + |
| 95 | +### Redefined or Overridden Variables: DOCKER_ENV_VARS_ALWAYS |
| 96 | + |
| 97 | +There is a corner case for the handling of `DOCKER_ENV_VARS`. If a variable is |
| 98 | +redefined (`+=`, `=`, `:=`) or overridden then the origin of the variable will be changed |
| 99 | +to `file` and there is no way of detecting in Make how it was set. |
| 100 | + |
| 101 | +If this happens after `docker.ink.mk` this is not an issue, but for all variables |
| 102 | +susceptible to be defined in the application `Makefile` this is indeed the case. |
| 103 | + |
| 104 | +A subset of these variables, namely variables relating to dependency resolution |
| 105 | +are therefore unconditionally passed to docker. The complete list can be found |
| 106 | +under `DOCKER_ENV_VARS_ALWAYS`. |
| 107 | + |
| 108 | +#### CFLAGS |
| 109 | + |
| 110 | +CFLAGS are not automatically passed to docker because they might contain spaces, |
| 111 | +'"' or other characters that will require escaping. The solution is to pass it with |
| 112 | +`DOCKER_ENVIRONMENT_CMDLINE` and escape every character as required. |
| 113 | + |
| 114 | +e.g: if wanting to override STRING_WITH_SPACES |
| 115 | + |
| 116 | +``` |
| 117 | +# normal build |
| 118 | +CFLAGS=-DSTRING_WITH_SPACES='\"with space\" make |
| 119 | +# in docker |
| 120 | +DOCKER_ENVIRONMENT_CMDLINE='-e CFLAGS=-DSTRING_WITH_SPACES=\'\\\"with\ space\\\"\'' \ |
| 121 | + BUILD_IN_DOCKER=1 make |
| 122 | +``` |
| 123 | + |
| 124 | +Alternatively, it is often easier to define the CFLAGS in the Makefile which gets |
| 125 | +evaluated inside the Docker image again), conditional on a less complex environment |
| 126 | +variable that gets added to `DOCKER_ENV_VARS` in the Makefile. |
| 127 | + |
| 128 | +[option-summary]: https://www.gnu.org/software/make/manual/html_node/Options-Summary.html |
0 commit comments