Skip to content

Commit 1c183aa

Browse files
committed
run-in-node-container: support rootless mode
run-in-node-container is used for javascript "rollup", so the tools running in the container produce files which must be owned by the user on the host. To achieve this, the docker run --user option is used to ensure that the tools in the container are run as host user. However, with rootless mode - apparently in both docker and podman, but I'm using podman - a user namespace is used and users in the container are mapped to a range of users on the host. This means that if we run a command as root in the container, this corresponds to the host user. When we specify --user, this results in a different host user being used. There are apparently two ways of achieving what we want - not using --user so that the commands run as root in the container, which is mapped to the desired host user. Or we can use --userns keep-id which means a 1:1 user mapping is used, and the user specified by --user corresponds to the same user on the host. The former seems more like how you'd typically use this mode. And so we detect rootless mode using "docker system info", and avoid the --user flag in this case. Podman reports "rootless: (true|false)", whereas docker just includes a "rootless" keyword. For more on this, see: https://www.redhat.com/sysadmin/user-flag-rootless-containers https://docs.docker.com/engine/security/rootless pre-commit/pre-commit#1243 pre-commit/pre-commit#1484 (Note: all of the above applies even without SELinux and was tested with "setenforce 0")
1 parent f214182 commit 1c183aa

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

hack/run-in-node-container.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,26 @@ elif ! (command -v docker >/dev/null); then
3333
echo "WARNING: docker not installed; please install docker or try setting NO_DOCKER=true" >&2
3434
exit 1
3535
fi
36+
37+
# We are running as the current host user/group so the files produced are
38+
# owned appropriately on the host.
39+
# With rootless mode, this happens without the need for a --user option.
40+
# https://www.redhat.com/sysadmin/user-flag-rootless-containers
41+
# Docker includes the "rootless" keyword in its "system info" output,
42+
# whereas podman includes "rootless: (true|false)".
43+
DOCKER_USER=""
44+
if ! "${DOCKER[@]}" system info | grep -q "rootless\(: true\)\?$"; then
45+
DOCKER_USER="--user $(id -u):$(id -g)"
46+
fi
47+
3648
# NOTE: yarn tries to read configs under $HOME and fails if it can't,
3749
# we don't need these configs but we need it to not fail.
3850
# We set HOME to somewhere read/write-able by any user, since our uid will not
3951
# exist in /etc/passwd in the node image and yarn will try to read from / and
4052
# fail instead if we don't.
41-
# We are running as the current host user/group so the files produced are
42-
# owned appropriately on the host.
4353
"${DOCKER[@]}" run \
4454
--rm -i \
45-
--user $(id -u):$(id -g) \
55+
${DOCKER_USER} \
4656
-e HOME=/tmp \
4757
-v "${REPO_ROOT:?}:${REPO_ROOT:?}" -w "${REPO_ROOT}" \
4858
"${NODE_IMAGE}" \

0 commit comments

Comments
 (0)