-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdock
More file actions
executable file
·85 lines (70 loc) · 2.27 KB
/
dock
File metadata and controls
executable file
·85 lines (70 loc) · 2.27 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
set -e -o pipefail
ENV_PASSTHROUGH=(
TERM
COLORTERM
LANG
)
die() {
[ "$#" -gt 0 ] && echo -e "\033[31m$*\033[0m" >&2
exit 1
}
docker_cmd=(docker)
[ -w /var/run/docker.sock ] || docker_cmd=(sudo docker)
findimg() {
local -a imgs
readarray -t imgs < <("${docker_cmd[@]}" images --format='{{ printf "%s:%s" .Repository .Tag }}')
declare -a match_prio
local i
for i in "${imgs[@]}"; do
case "$i" in
"$1") [ -z "${match_prio[1]}" ] && match_prio[1]="$i" ;; # Exact match
"$1":*) [ -z "${match_prio[2]}" ] && match_prio[2]="$i" ;; # Repo match
*/"$1") [ -z "${match_prio[3]}" ] && match_prio[3]="$i" ;; # Repo (last element) + Tag match
*/"$1":*) [ -z "${match_prio[4]}" ] && match_prio[4]="$i" ;; # Repo (last element) match
esac
done
for i in "${match_prio[@]}"; do
[ -n "$i" ] && echo "$i" && return 0
done
}
IMG=archlinux:base-devel
BLACKLIST_DIRS=":/:/boot:/proc:/root:/run:/etc:/usr:/dev:/var:/home:/opt:/srv:/tmp:/mnt:/sys:$(realpath "$HOME"):"
cwd="$(realpath .)"
echo ":$BLACKLIST_DIRS:" | grep -qF ":$cwd:" && die "Refuse to run under protected directory: $cwd"
# Parse and generate additional docker arguments
docker_args=()
while [ "$#" -gt 0 ]; do
case "$1" in
--host) docker_args+=(--network host) ;;
--gpu) docker_args+=(--gpus all) ;;
--root) docker_args+=(--user root) ;;
*) break ;;
esac
shift
done
[ -n "$1" ] && IMG=$(findimg "$1")
[ -z "$IMG" ] && die "image not found: $1"
shift || true
# Environment variables
for e in "${ENV_PASSTHROUGH[@]}"; do
[ -z "${!e}" ] || docker_args+=(-e "$e=${!e}")
done
if [ -n "$TZ" ]; then
docker_args+=(-e TZ="$TZ")
else
tz=$(readlink -f /etc/localtime | sed -E 's|.*/zoneinfo/(posix/\|right/)?||')
docker_args+=(-e TZ="${tz:-UTC}")
fi
# Mounts
docker_args+=(-v "$cwd:/cwd")
case "$IMG" in
*archlinux*)
[ -f "/etc/pacman.d/mirrorlist" ] && docker_args+=(-v /etc/pacman.d/mirrorlist:/etc/pacman.d/mirrorlist:ro)
;;
esac
set -x
exec "${docker_cmd[@]}" run -it --rm --workdir /cwd --tmpfs /tmp:exec --cpu-shares 512 \
"${docker_args[@]}" \
--entrypoint /bin/sh "$IMG" \
-c "[ -x /bin/bash ] && exec /bin/bash || exec /bin/sh"