forked from Kazetsukai/onetech
-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker-run.sh
executable file
·69 lines (57 loc) · 1.92 KB
/
docker-run.sh
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
#!/bin/bash
# Usage: run-docker [cmd [arg0 [arg1 ...]]]
#
# Maps ~/.ssh and ~/.npm in the Docker container
# ~/.ssh is simply passed through, so the build can use the user's SSH credentials when needed.
# ~/.npm is mapped to a temporary directory, deleted at the end of the script, to keep usage isolated and clean.
# Note that npm stores its cache in the .npm directory, so it could perhaps be made
# durable and save a bit of build time, but the time difference is small.
image="${IMAGE:-"twotech-build_env"}"
tag="${TAG:-"latest"}"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ "$(docker images -q "${image}":"${tag}" 2> /dev/null)" == "" ]]; then
echo "Docker image $image:$tag does not exist. Running script to create it."
# if [[ $? -ne 0 ]]; then
if ! "${SCRIPT_DIR}"/docker-build_env.sh; then
echo "Could not create Docker image! Exiting..."
exit 1
fi
fi
echo "Sharing directory $SCRIPT_DIR"
# Make temporary storage for ~/.npm, since earlier versions of npm store
# stuff in ~/.npm and can't be persuaded otherwise. Clean up when done.
BUILD_TMP=""
function cleanup() {
echo "Cleaning up"
if [ -n "$BUILD_TMP" ]; then
rm -rf "$BUILD_TMP"
fi
}
function trapcmd() {
trapStatus=$?
if [ $trapStatus -eq 0 ]; then
trapStatus=1
fi
cleanup
exit $trapStatus
}
trap "trapcmd" HUP TERM INT QUIT TRAP ABRT SEGV
if [ -z "$TMPDIR" ]; then
TMPDIR=/tmp
fi
BUILD_TMP="$(mktemp -d "$TMPDIR/twotech_build.XXXX")"
echo "Putting temp files in $BUILD_TMP"
BUILD_TMP_NPM="$BUILD_TMP/.npm"
mkdir -p "$BUILD_TMP_NPM"
docker run -it --rm \
--env LINUX_USER="$(id -un)" \
--env LINUX_UID="$(id -u)" \
--env LINUX_GROUP="$(id -gn)" \
--env LINUX_GID="$(id -g)" \
--env LINUX_DIR="$PWD" \
--env TERM="$TERM" \
--mount "src=$SCRIPT_DIR,target=$SCRIPT_DIR,type=bind" \
--mount "src=${HOME}/.ssh,target=${HOME}/.ssh,type=bind" \
--mount "src=${BUILD_TMP_NPM},target=${HOME}/.npm,type=bind" \
"$image:$tag" "$@"
cleanup