-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·95 lines (77 loc) · 2.43 KB
/
start.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
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
#!/bin/bash
# basic settings
WORK_DIR=$(pwd)
PREFIX=ml
GROUP=aiteam
#
# you probably don't need to modify anything below this line.
#
USR_ID=$(id -u)
GRP_ID=$(getent group $GROUP | cut -d: -f 3)
IMAGE="$USER/$PREFIX"
CONTAINER_NAME=$(echo "${PREFIX}_${USER}${WORK_DIR}" | tr \/ _)
AMOUNT_IMAGES=$(docker images "$IMAGE" | wc -l)
SCRIPTPATH="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit 1
pwd -P
)"
if [ "$AMOUNT_IMAGES" -le 1 ]; then
#
# only build the image if it's not already existing.
#
echo "Image $IMAGE not found on local docker daemon. Now building..."
docker build -t "$IMAGE" \
--build-arg USER="$USER" \
--build-arg USER_ID="$USR_ID" \
--build-arg GROUP_ID="$GRP_ID" \
"$SCRIPTPATH"
else
echo "Image $IMAGE found on local docker daemon."
fi
AMOUNT_CONTAINERS=$(docker ps -a --quiet --filter="name=^/$CONTAINER_NAME$" | wc -l)
if [ "$AMOUNT_CONTAINERS" -eq 0 ]; then
#
# only create container if not already existing for path.
#
echo "Creating a new ML container for user $USER and work path $WORK_DIR"
docker create -it --name "$CONTAINER_NAME" --runtime=nvidia \
-u "$USR_ID:$GRP_ID" \
--workdir="/home/$USER" \
--volume="/etc/group:/etc/group:ro" \
--volume="/etc/passwd:/etc/passwd:ro" \
--volume="/etc/shadow:/etc/shadow:ro" \
--mount type=bind,source="$WORK_DIR",target=/opt/work \
--mount type=bind,source="/srv/datasets",target=/opt/data \
--network host \
--shm-size=172gb \
"$IMAGE" &>/dev/null
fi
CONTAINER_ID=$(docker ps --quiet --filter="name=^/$CONTAINER_NAME$")
if [ -z "$CONTAINER_ID" ]; then
#
# start created container
#
echo "Starting the container..."
docker start "$CONTAINER_NAME" &>/dev/null
CONTAINER_ID=$(docker ps --quiet --filter="name=^/$CONTAINER_NAME$")
fi
if [ -n "$NVIDIA_VISIBLE_DEVICES" ]; then
USE_GPUS="$NVIDIA_VISIBLE_DEVICES"
fi
echo "Connecting to the running container..."
echo ""
docker exec -e NVIDIA_VISIBLE_DEVICES="$USE_GPUS" -it "$CONTAINER_NAME" /bin/bash
echo ""
AMOUNT_PS_LINES=$(docker top "$CONTAINER_NAME" -e | wc -l)
if [ "$AMOUNT_PS_LINES" -le 2 ]; then
#
# stopping the container if no user processes are running anymore.
#
docker kill "$CONTAINER_NAME" &>/dev/null
echo "Stopped the container."
echo "Your container has been persisted to the docker daemon."
echo ""
echo "If you don't need the container anymore, please delete it"
echo "by running: docker rm $CONTAINER_ID"
echo ""
fi