Skip to content

Instantly share code, notes, and snippets.

@tstone2077
Last active September 30, 2019 01:16
Show Gist options
  • Save tstone2077/a4ba95280dc679c088f8db3a13153a19 to your computer and use it in GitHub Desktop.
Save tstone2077/a4ba95280dc679c088f8db3a13153a19 to your computer and use it in GitHub Desktop.
Docker entrypoint shell script that updates the docker user's uid to that of a specific directory, allowing that directory to be writable.
#!/bin/bash
# Docker entrypoint file to set the user's uid to an id that can write to
# a specific directory. Note: entrypoint must be run as root
#
# Usage:
# Set MAIN_USERNAME to the username that you want the container to run as
# Set MAIN_DIRECTORY to the directory that you want the user to be able to write
MAIN_USERNAME=username
MAIN_DIRECTORY=/home/username/project
# Any other entrypoint can be included or added at the bottom
# ==================================================
TARGET_GID=$(stat -c "%g" $MAIN_DIRECTORY)
TARGET_UID=$(stat -c "%u" $MAIN_DIRECTORY)
cat /etc/group | grep $TARGET_GID > /dev/null 2>&1
# Create new group using target GID and add nobody user
if [ $? -ne 0 ]; then
groupadd -g $TARGET_GID tempgroup
usermod -u $TARGET_UID -a -G tempgroup $MAIN_USERNAME
else
# GID exists, find group name and add
GROUP=$(getent group $TARGET_GID | cut -d: -f1)
usermod -u $TARGET_UID -a -G $GROUP $MAIN_USERNAME
fi
sudo -u $MAIN_USERNAME exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment