-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-shell
executable file
·57 lines (47 loc) · 1.14 KB
/
docker-shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
#
# Copyright (c) 2017 Gaël PORTAY <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the MIT License.
#
set -e
usage() {
cat <<EOF
Usage: ${0##*/} [OPTIONS] [--] [COMMAND] [ARG...]
${0##*/} [OPTIONS] -c COMMAND [ARG...]
Run a shell as user in a new container and bind mount cwd
Options:
--help Print usage
EOF
}
docker_build() {
cat "$1" - <<EOF | docker build --tag "$2" -
RUN groupadd --non-unique --gid $GROUPS $USER
RUN useradd --non-unique --gid $GROUPS --uid $UID --create-home --home-dir /home/$USER --shell $SHELL $USER
EOF
}
opts="--rm --volume $PWD:/home/$USER"
while [ "$#" -ne 0 ]; do
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
elif [ "$1" = "-c" ] || [ "$1" = "--" ]; then
shift
break
else
break
fi
shift
done
hash="$(realpath "Dockerfile" | sha256sum - | cut -d' ' -f1)"
tag="dsh-$hash"
docker_build "Dockerfile" "$tag" >&2
if [ "$#" -eq 0 ]; then
opts+=" --tty"
opts+=" --interactive"
set -- su -l $USER
else
set -- su -l $USER -c "$*"
fi
exec docker run $opts "$tag" "$@"