Skip to content

Commit ce47f32

Browse files
committed
fix: thread UID/GID through Docker
When running Git commands inside this Docker container (i.e., commands that the `version.py` script needs for determining version information), the Docker build would run into issues like: ``` fatal: detected dubious ownership in repository at '/workspace' To add an exception for this directory, call: git config --global --add safe.directory /workspace ``` This is due to an extra Git check that detects that the Docker user is not the same one who owns the `.git` directory of this project. After looking into this, the best solution the internet has to offer is to thread the current user's UID and GID through the Docker image (i.e., the new `builder` user) and then `docker run --user ...`. This both avoids the Git check but also seems to be considered a best practice in some circles (?).
1 parent 91c48f0 commit ce47f32

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

Dockerfile

+21-11
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,32 @@
33
# Here we choose Bionic 18.04.
44
FROM ubuntu:bionic
55

6+
# We want to use the same UID/GID of the external user to avoid permission
7+
# issues. See the user setup at the end of the file.
8+
ARG UID=1000
9+
ARG GID=1000
10+
611
RUN apt-get update \
7-
&& apt-get install -y --no-install-recommends \
8-
ccache \
9-
curl \
10-
ca-certificates \
11-
build-essential \
12-
clang \
13-
python3 \
14-
git \
15-
ninja-build \
16-
&& apt-get clean \
17-
&& rm -rf /var/lib/apt/lists/*
12+
&& apt-get install -y --no-install-recommends \
13+
ccache \
14+
curl \
15+
ca-certificates \
16+
build-essential \
17+
clang \
18+
python3 \
19+
git \
20+
ninja-build \
21+
&& apt-get clean \
22+
&& rm -rf /var/lib/apt/lists/*
1823

1924
RUN curl -sSLO https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1-linux-x86_64.tar.gz \
2025
&& tar xf cmake-3.25.1-linux-x86_64.tar.gz \
2126
&& rm cmake-3.25.1-linux-x86_64.tar.gz \
2227
&& mkdir -p /opt \
2328
&& mv cmake-3.25.1-linux-x86_64 /opt/cmake
2429
ENV PATH /opt/cmake/bin:$PATH
30+
31+
RUN groupadd -g ${GID} builder && \
32+
useradd --create-home --uid ${UID} --gid ${GID} builder
33+
USER builder
34+
WORKDIR /workspace

docker_build.sh

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
set -ex
33

44
echo "Building the docker image"
5-
docker build -t wasi-sdk-builder:latest .
5+
docker build \
6+
--build-arg UID=$(id -u) --build-arg GID=$(id -g) \
7+
-t wasi-sdk-builder:latest .
68

79
echo "Building the package in docker image"
810
mkdir -p ~/.ccache
9-
docker run --rm --user $(id -u):$(id -g) -v "$PWD":/workspace:Z -v ~/.ccache:/root/.ccache:Z -e NINJA_FLAGS=-v --workdir /workspace --tmpfs /tmp:exec wasi-sdk-builder:latest make package LLVM_CMAKE_FLAGS=-DLLVM_CCACHE_BUILD=ON
11+
docker run --rm \
12+
--user $(id -u):$(id -g) \
13+
-v "$PWD":/workspace:Z \
14+
-v ~/.ccache:/home/builder/.ccache:Z \
15+
-e NINJA_FLAGS=-v \
16+
--tmpfs /tmp:exec \
17+
wasi-sdk-builder:latest \
18+
make package LLVM_CMAKE_FLAGS=-DLLVM_CCACHE_BUILD=ON

0 commit comments

Comments
 (0)