Skip to content

Instantly share code, notes, and snippets.

@howird
Last active March 13, 2024 19:11
Show Gist options
  • Save howird/1314a3821338e2bd500919da4064d0fe to your computer and use it in GitHub Desktop.
Save howird/1314a3821338e2bd500919da4064d0fe to your computer and use it in GitHub Desktop.
Create a new user with your uid and groups in a docker container and change the owner of the provided directory to the new user.
#!/bin/bash
# This script is useful if you have a directory whose contents are owned by a root user within
# a docker container. This makes it difficult to delete or modify that directory without sudo
# priviledges. Example usage to quickly make yourself the owner of a directory:
# - start a docker container (if needed) mapping a folder to a dir called `/to-own` in the container:
# docker run -d -v <path to desired folder>:/to-own --name tmp-cntnr debian:stable-slim sleep inf
# - run the script, specifying the name/id of the container and the folder to `chown`:
# ./docker-root-script.sh tmp-cntnr /to-own
# - delete the container:
# docker container rm --force tmp-cntnr
# Alternatively, if you just want a script that automatically creates a user with your uid's and
# gid's in your container (if they don't already exist) and then `docker exec`'s into it as that
# user, comment out the `chown` command (lines 77-78), and uncomment the last line (line 81)
# Then, you can run this script with the command: `./docker-root-script.sh <container id> bash`
# Or, if you are in a directory with a docker-compose.yml file: `./docker-root-script.sh bash`
print_usage() {
echo "Usage: $0 <docker container id> <path to chown in docker container>"
echo " $0 <path to chown in docker container>"
exit 1
}
if [ "$#" -eq 2 ]; then
if [ "$(docker ps -q -f id=$1)" ]; then
echo Found container with id: $1
container_id="$1"
else
container_id="$(docker ps -q -f name=$1)"
if [ ! $container_id ]; then
echo ERROR: Docker container with id or name $1 not found.
print_usage
fi
echo Found container named: $1 with id: $container_id
fi
p="${@:2}"
elif [ "$#" -eq 1 ]; then
if [ ! -f ./docker-compose.yml ]; then
echo "ERROR: If no container id is provided, a docker-compose.yml file must exist in the current directory."
print_usage
fi
container_id="$(docker compose ps -q | head -n 1)"
if [ ! $container_id ]; then
echo "ERROR: No containers in ./docker-compose.yml are running."
print_usage
fi
p="${@:1}"
else
print_usage
fi
my_uid=$(id -u)
# check if current user has been created in container
if ! docker exec $container_id bash -c "getent passwd "$my_uid"" > /dev/null; then
# create user
echo "Setting up user \"user\" with uid: $(id -u) and gids: $(id -G)"
docker exec $container_id bash -c "useradd --create-home --no-log-init -u "$(id -u)" user"
# create each of the current user's groups in docker container
groups=($(id -G))
for ((i = 0; i < ${#groups[@]}; i++)); do
group_info=$(getent group ${groups[i]})
group_name=${group_info%%:*}
docker exec $container_id bash -c "groupadd -g "${groups[i]}" $group_name"
done
# add all the created docker groups to docker user
docker exec $container_id bash -c "usermod -a -G $(id -G | sed 's/ /,/g') user"
else
echo User with uid, $my_uid, already created!
fi
# recursive chown of provided path if it exists
docker exec -u 0 $container_id bash -c \
"[ -e $p ] && chown -R $my_uid:$(id -g) $p && echo Success! Folder is now owned by current user. || echo Provided path does not exist in the container."
# if you just want to enter the container as the new user, delete the last command and uncomment:
# echo Entering container. && docker exec -it -u $my_uid $container_id $p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment