-
Notifications
You must be signed in to change notification settings - Fork 0
/
runimagelocal
executable file
·116 lines (92 loc) · 2.42 KB
/
runimagelocal
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
function Help()
{
if [[ $1 != "" ]]; then
echo "Error: $*"
fi
if [[ -n "${SHORT_HELP_MODE}" ]]; then
echo "-u <user> -i <image name to upload> -n <docker repository name> -r <registry> : upload docker image to public registry."
exit 1
fi
cat <<EOF
Usage: $0 -i <image name> [ -s <shell> ] [ -m <mount_dir>]
Runs a container interactively with the docker image, by default using the shell /bin/sh.
Can override the shell with the -s option)
The HOME directory is mounted to /var/home in the container (can override with -m option)
EOF
exit 1
}
function get_platform() {
local uname
uname=$(uname -m)
if [[ $uname == *arm64* ]] || [[ $uname == *aarch64* ]]; then
PLATFORM="arm64"
else
if [[ $uname == *x86_64* ]] || [[ $uname == *amd64* ]]; then
PLATFORM="amd64"
else
echo "os type $uname is not supported"
exit 1
fi
fi
}
function dockerrunimagebash {
local PLAT
if [[ "$IMAGE_NAME" == "" ]]; then
if [[ "$PLATFORM" == "arm64" ]]; then
IMAGE_NAME="arm64v8/alpine:latest"
fi
if [[ "$PLATFORM" == "amd64" ]]; then
IMAGE_NAME="alpine:latest"
fi
else
# would be cleaner to do jq, but adds another dependency.
local IMAGE_ARCH
echo "Pulling ${IMAGE_NAME} ..."
docker pull "$IMAGE_NAME"
IMAGE_ARCH=$(docker image inspect "$IMAGE_NAME" | jq -r 'first | .Architecture')
if [[ $IMAGE_ARCH != "$PLATFORM" ]]; then
local OS
OS=$(docker image inspect "$IMAGE_NAME" | jq -r 'first | .Os')
PLAT="--platform $OS/$IMAGE_ARCH"
fi
fi
cat <<EOF
***
image: $IMAGE_NAME
shell: $SHELL_EXE
PLATFORM: ${PLAT:-native}
Mounting directory: $MOUNT_DIR to /var/home in container
***
EOF
docker run $PLAT --rm -it --entrypoint "$SHELL_EXE" -v "$MOUNT_DIR:/var/home" "$IMAGE_NAME" $OTHERARGS
}
SHELL_EXE="/bin/sh"
MOUNT_DIR="$HOME"
while getopts "hvi:s:m:" opt; do
case ${opt} in
h)
Help
;;
m)
MOUNT_DIR=$OPTARG
;;
i)
IMAGE_NAME=$OPTARG
;;
s)
SHELL_EXE=$OPTARG
;;
v)
set -x
export PS4='+(${BASH_SOURCE}:${LINENO})'
;;
*)
Help "Invalid option"
;;
esac
done
shift $((OPTIND-1))
OTHERARGS="$@"
get_platform
dockerrunimagebash