-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker image: switch to a new non-root user when started as root
Instead of manually adding a matching user with the docker-adduser cheribuild target, this adds an ENTRYPOINT script to the docker image that automatically creates an unprivileged user. The UID/GID/name can be passed using environment variables (-e flag to `docker run`).
- Loading branch information
1 parent
3d04b6b
commit 8b0188a
Showing
2 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/sh -e | ||
|
||
if [ "$(id -u)" != 0 ]; then | ||
echo "Already running as non-root, can't change user." | ||
exec "$@" | ||
fi | ||
# Create a non-root user with UID/GID matching the host user to ensure that | ||
# files written to the volumes are not owned by root. | ||
: "${cheribuild_uid:=1234}" | ||
: "${cheribuild_gid:=1234}" | ||
: "${cheribuild_user:=cheri}" | ||
addgroup --quiet --gid ${cheribuild_gid} "${cheribuild_user}" | ||
yes | adduser --quiet --uid ${cheribuild_uid} --disabled-password --ingroup "${cheribuild_user}" "${cheribuild_user}" > /dev/null | ||
|
||
# Run the actual command: | ||
export HOME="/home/${cheribuild_user}" | ||
exec gosu "${cheribuild_user}" "$@" |