Skip to content

Commit 2214e7e

Browse files
committed
Fix git security issue with docker build
Make the docker user id and group id match the running user's, so that files built by the container are seen as owned by the current user. Also set the current directory as "safe" in git so it's able to inspect the git history. _note_: this will successfully run in a rootless docker install, but the created files will have random uid/guids. You'll have to sudo to delete them or chown them. For normal docker installs there should be no issues. See [this issue](moby/moby#41497) to track the rootless problem on the docker side.
1 parent 9ee63e9 commit 2214e7e

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Dockerfile

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
FROM ubuntu:focal
22

3+
ARG USER_ID
4+
ARG GROUP_ID
35
ENV DEBIAN_FRONTEND noninteractive
46

5-
RUN apt-get update && apt-get install -y git cmake make ruby gcc python3 python3-pip
7+
RUN apt-get update && apt-get install -y git cmake make ruby gcc python3 python3-pip gcc-arm-none-eabi
68

79
RUN pip install pyyaml
810

9-
RUN useradd inav
11+
# if either of these are already set the same as the user's machine, leave them be and ignore the error
12+
RUN addgroup --gid $GROUP_ID users; exit 0;
13+
RUN adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user; exit 0;
1014

11-
USER inav
15+
USER user
16+
RUN git config --global --add safe.directory /src
1217

1318
VOLUME /src
1419

build.sh

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,29 @@ fi
2121

2222
if [ -z "$(docker images -q inav-build)" ]; then
2323
echo -e "*** Building image\n"
24-
docker build -t inav-build .
24+
docker build -t inav-build --build-arg USER_ID="$(id -u)" --build-arg GROUP_ID="$(id -g)" .
2525
echo -ne "\n"
2626
fi
2727

2828
if [ ! -d ./build ]; then
2929
echo -e "*** Creating build directory\n"
30-
mkdir ./build
30+
mkdir ./build && chmod 777 ./build
31+
fi
32+
33+
if [ ! -d ./downloads ]; then
34+
echo -e "*** Creating downloads directory\n"
35+
mkdir ./downloads && chmod 777 ./downloads
36+
fi
37+
38+
if [ ! -d ./tools ]; then
39+
echo -e "*** Creating tools directory\n"
40+
mkdir ./tools && chmod 777 ./tools
3141
fi
3242

3343
echo -e "*** Building targets [$@]\n"
3444
docker run --rm -it -v "$(pwd)":/src inav-build $@
3545

36-
if ls ./build/*.hex &> /dev/null; then
46+
if [ -z "$(ls ./build/*.hex &> /dev/null)" ]; then
3747
echo -e "\n*** Built targets in ./build:"
3848
stat -c "%n (%.19y)" ./build/*.hex
3949
fi

0 commit comments

Comments
 (0)