Skip to content

Commit 81799d8

Browse files
authored
Merge pull request #266 from crasbe/pr/fix_usergroup
fix user group setting for `git-cache-rs`, add documentation
2 parents 2744b9c + 5ac2ab1 commit 81799d8

7 files changed

Lines changed: 139 additions & 31 deletions

File tree

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
11
# riotbuild
22
Dockerfiles for creating build environment for building RIOT projects.
3+
4+
## Building your own images
5+
6+
The RIOT build system containers are staggered. The foundation is set by
7+
`riotdocker-base`, which sets up the user inside of the container and
8+
sets traps for `SIGINT` and `SIGTERM` signals.
9+
10+
You can build the image with the following command:
11+
12+
```sh
13+
docker build --pull -t riotdocker-base ./riotdocker-base/
14+
```
15+
16+
The second image, `static-test-tools`, builds upon `riotdocker-base` and
17+
contains all of the tools that are required to run the RIOT static tests.
18+
This image is also used by the `static-test` GitHub workflow in the main
19+
`RIOT` repository.
20+
21+
You can build the image with the following command. Setting the
22+
`DOCKER_REGISTRY` argument ensures that the local copy of the container is used
23+
instead of the upstream version. You can omit this parameter if you haven't
24+
made any changes to `riotdocker-base`.
25+
26+
***NOTE:*** If docker complains about not finding the image, you can try to
27+
set the `DOCKER_REGISTRY` argument to `localhost` instead.
28+
29+
```sh
30+
docker build --build-arg DOCKER_REGISTRY=docker.io/library -t static-test-tools ./static-test-tools/
31+
```
32+
33+
The third image, `riotbuild`, builds upon the `static-test-tools` and contains
34+
the full build environment required to build all platforms in `RIOT`.
35+
This container is rather big (>10GB) and takes a good while to build.
36+
37+
You can run the following command to build it. Again, the `DOCKER_REGISTRY`
38+
command is optional if you haven't made any changes to `static-test-tools`.
39+
40+
```sh
41+
docker build --build-arg DOCKER_REGISTRY=docker.io/library -t riotbuild ./riotbuild/
42+
```
43+
44+
The fourth image, `murdock-worker`, builds upon `riotbuild` and contains
45+
everything that is used by the CI and can be built with the following command.
46+
Again, the `DOCKER_REGISTRY` command is optional if you haven't made any
47+
changes to `riotbuild`.
48+
49+
```sh
50+
docker build --build-arg DOCKER_REGISTRY=docker.io/library -t murdock-worker ./murdock-worker/
51+
```
52+
53+
## Testing your changes
54+
55+
Before you can test your changes, you have to find out the Image ID of your
56+
freshly baked container. For example, if you want to search for `riotbuild`,
57+
you can have docker list all containers that match that name.
58+
59+
```sh
60+
riotdocker$ docker image list riotbuild
61+
REPOSITORY TAG IMAGE ID CREATED SIZE
62+
riotbuild latest f610ef8e4bbd 19 minutes ago 14.9GB
63+
```
64+
65+
Depending on your changes and what you want to test, you can either start a
66+
shell inside of the container with the following command:
67+
68+
```sh
69+
docker run --rm --user $(id -u):$(id -g) -it f610ef8e4bbd bash
70+
riotbuild@f610ef8e4bbd:~$
71+
```
72+
73+
Or you can pass your image to the RIOT build system and build an application
74+
or test of your liking:
75+
76+
```sh
77+
BUILD_IN_DOCKER=1 DOCKER_IMAGE=f610ef8e4bbd BOARD=nrf52840dk make -C tests/sys/shell
78+
```

murdock-worker/Dockerfile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#
21
# RIOT Murdock Dockerfile
32
#
43
# the resulting image is being used in RIOT's CI (Murdock)
@@ -29,10 +28,6 @@ RUN pip3 install hiredis
2928
# install testrunner dependencies
3029
RUN pip3 install click
3130

32-
# get git-cache-rs binary
33-
COPY --from=ghcr.io/kaspar030/git-cache:0.1.5-jammy /git-cache /usr/bin/git-cache
34-
ENV GIT_CACHE_RS=/usr/bin/git-cache
35-
3631
# install newer ccache package
3732
ARG CCACHE_TGZ=ccache-4.7.4-linux-x86_64.tar.xz
3833
COPY files/${CCACHE_TGZ} /
@@ -44,6 +39,9 @@ COPY murdock_slave.sh /usr/bin/murdock_slave
4439
# create cache folder
4540
RUN mkdir -m777 /cache
4641

42+
# remove old git-cache-rs files from before the directory structure changed
43+
RUN rm -rf /cache/.gitcache/*.git /cache/.gitcache/*.lock
44+
4745
# set cache folder for Download Cache
4846
ENV DLCACHE_DIR=/cache/.dlcache
4947

riotbuild/Dockerfile

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1+
# `riotbuild` Dockerfile
12
#
2-
# RIOT Dockerfile
3-
#
4-
# The resulting image will contain everything needed to build RIOT for all
5-
# supported platforms. This is the largest build image, it takes about 1.5 GB in
6-
# total.
7-
#
8-
# Setup:
9-
# 1. Install docker, add yourself to docker group, enable docker, relogin
10-
#
11-
# Use prebuilt image:
12-
# 1. Prebuilt image can be pulled from Docker Hub registry with:
13-
# # docker pull riot/riotbuild
14-
#
15-
# Use own build image:
16-
# 1. Build own image based on latest base OS image (from the riotbuild directory):
17-
# # docker build --pull -t riotbuild .
18-
#
19-
# Usage:
20-
# 1. cd to riot root
21-
# 2. # docker run -i -t -u $UID -v $(pwd):/data/riotbuild riotbuild ./dist/tools/compile_test/compile_test.py
3+
# This container includes the whole build toolchain required to build all
4+
# platforms supported by RIOT.
5+
226
ARG DOCKER_REGISTRY="docker.io/riot"
237
FROM ${DOCKER_REGISTRY}/static-test-tools:latest
248

@@ -326,6 +310,11 @@ RUN \
326310
# get laze binary
327311
COPY --from=kaspar030/laze:0.1.20-jammy /laze /usr/bin/laze
328312

313+
# get git-cache-rs binary and set the environment variable for
314+
# the RIOT package subsystem
315+
COPY --from=ghcr.io/kaspar030/git-cache:0.2.8-jammy /git-cache /usr/bin/git-cache
316+
ENV GIT_CACHE_RS=/usr/bin/git-cache
317+
329318
# get Dockerfile version from build args
330319
ARG RIOTBUILD_VERSION=unknown
331320
ENV RIOTBUILD_VERSION=$RIOTBUILD_VERSION

riotdocker-base/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# `riotdocker-base` Dockerfile
2+
#
3+
# This container sets the foundation for all subsequent containers and
4+
# initializes the user environment.
5+
16
FROM ubuntu:jammy
27

38
LABEL maintainer="Kaspar Schleiser <kaspar@riot-os.org>"
@@ -20,7 +25,7 @@ RUN \
2025
COPY create_user.c /tmp/create_user.c
2126
RUN gcc -DHOMEDIR=\"/data/riotbuild\" -DUSERNAME=\"riotbuild\" /tmp/create_user.c -o /usr/local/bin/create_user \
2227
&& chown root:root /usr/local/bin/create_user \
23-
&& chmod u=rws,g=x,o=- /usr/local/bin/create_user \
28+
&& chmod u=rws,g=x,o=x /usr/local/bin/create_user \
2429
&& rm /tmp/create_user.c
2530

2631
# Create working directory for mounting the RIOT sources

riotdocker-base/create_user.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* Docker runs containers with local root privileges. That means, that all
3+
* accesses to shared directories and files will be performed as root, leading
4+
* to possibly inaccessible files and files with the wrong owner (root instead
5+
* of the local user).
6+
*
7+
* Docker allows to set the user ID and user group with the `--user` argument
8+
* when running a Docker container. That argument however only sets the
9+
* respective IDs and not the user- and groupname.
10+
*
11+
* Therefore, this file is compiled as a binary that is executed by `run.sh`
12+
* every time a Docker container (or child of the `riotdocker-base` container)
13+
* is executed. It sets the home directory, user- and groupnames and UID:GID.
14+
*
15+
* The username and groupname is set to `riotbuild`, but they are just aliases,
16+
* as the underlying rights mechanism only checks the IDs and not the names.
17+
*/
18+
119
#include <stdio.h>
220
#include <stdlib.h>
321
#include <sys/types.h>
@@ -12,9 +30,16 @@ int main(int argc, char *argv[])
1230
setuid(0);
1331

1432
unsigned uid = atoi(argv[1]);
33+
unsigned gid = atoi(argv[2]);
1534
char buf[128];
1635

17-
sprintf(buf, "/usr/sbin/useradd -u %u -d %s -r -g 0 -N %s", uid, HOMEDIR, USERNAME);
36+
/* create the usergroup */
37+
sprintf(buf, "/usr/sbin/groupadd -g %u %s", gid, USERNAME);
38+
system(buf);
39+
40+
/* set the UID, Home Directory, User Group */
41+
sprintf(buf, "/usr/sbin/useradd -u %u -d %s -g %u %s", uid, HOMEDIR, gid, USERNAME);
1842
system(buf);
43+
1944
return 0;
2045
}

riotdocker-base/run.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ runcommand() {
2626
return "$retval"
2727
}
2828

29-
# create passwd entry for current uid, fix HOME variable
30-
# only execute, if the current uid does not exist.
31-
if ! id $(id -u) >/dev/null 2>/dev/null; then
32-
create_user $(id -u)
29+
# Create passwd entry with the UID and GID of the user running the
30+
# `riotdocker-base` container and any containers derived from it.
31+
# It also sets the HOME variable.
32+
# Only execute, if the current UID does not exist.
33+
if ! id "$(id -u)" >/dev/null 2>/dev/null; then
34+
if [ "$(id -u)" -ne 0 ] && [ "$(id -g)" -eq 0 ]; then
35+
# Fallback to UID:UID if the container is run without setting a GID
36+
echo -e "\e[33mWarning: The Docker User ID is $(id -u), but the" \
37+
"Group ID is 0 (root), update your RIOT repository or check" \
38+
"the Docker call!\e[0m"
39+
create_user "$(id -u)" "$(id -u)"
40+
else
41+
create_user "$(id -u)" "$(id -g)"
42+
fi
3343
fi
3444
export HOME=/data/riotbuild
3545

static-test-tools/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# `static-test-tools` Dockerfile
2+
#
3+
# This container has all the dependencies required to execute the static tests
4+
# for the RIOT sources.
5+
16
ARG DOCKER_REGISTRY="docker.io/riot"
27
FROM ${DOCKER_REGISTRY}/riotdocker-base:latest
38

0 commit comments

Comments
 (0)