Last active
July 23, 2021 02:09
-
-
Save plattrap/a5871728fd37761595653c6c2b19bccd to your computer and use it in GitHub Desktop.
Run as me inside docker
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
#!/bin/bash | |
# Create a temporary directory and store its name in a variable ... | |
TMPDIR=$(mktemp -d) | |
# Bail out if the temp directory wasn't created successfully. | |
if [[ ! -d "$TMPDIR" ]]; then | |
>&2 echo "Failed to create temp directory" | |
exit 1 | |
fi | |
# deletes the temp directory | |
function cleanup { | |
rm -rf "$TMPDIR" | |
echo "Deleted temp working directory $TMPDIR" | |
} | |
# register the cleanup function to be called on the EXIT signal | |
trap cleanup EXIT | |
# Now do real work | |
# extract command line | |
# e.g. ./docker_me.sh "--rm -it -v `pwd`:`pwd` -w `pwd` -e HOME=`pwd`" ubuntu "id; pwd; ls -al" | |
OPTIONS="$1" | |
IMAGE="$2" | |
COMMAND="$3" | |
echo "Creating identity in temp directory $TMPDIR" | |
TMP_IMAGE=$(docker create $IMAGE) | |
docker cp $TMP_IMAGE:/etc/passwd $TMPDIR | |
docker cp $TMP_IMAGE:/etc/group $TMPDIR | |
docker container rm $TMP_IMAGE >/dev/null | |
# Copy just this user's ID across | |
#grep "$USER:x:$(id -u $USER):" /etc/passwd | tee -a $TMPDIR/passwd | |
#grep "$USER:x:$(id -g $USER):" /etc/group | tee -a $TMPDIR/group | |
# Pull everyone across, append the local names to the images | |
cat /etc/passwd >> $TMPDIR/passwd | |
cat /etc/group >> $TMPDIR/group | |
# Get my PID and GID | |
PID_GID="$(id -u $USER):$(id -g $USER)" | |
# print out the command line | |
( set -x | |
docker run --user $PID_GID -v "$TMPDIR"/passwd:/etc/passwd -v "$TMPDIR"/group:/etc/group \ | |
$OPTIONS "$IMAGE" /bin/sh -c "$COMMAND" | |
) | |
# Cleanup will happen now. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment