This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdocker-cmd-linux.sh
More file actions
executable file
·174 lines (164 loc) · 4.68 KB
/
docker-cmd-linux.sh
File metadata and controls
executable file
·174 lines (164 loc) · 4.68 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# Example of docker usage for RLLIB + SUMO Utlis
#
# Author: Lara CODECA
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0.
set -e
set -u
IMAGE_NAME="tf-gpu-sumo-$(date +%Y-%m-%d)"
IMAGE_FOLDER="docker-image-linux"
GPU=true
GPU_OPT="--gpus all"
OPTIRUN=false
OPTIRUN_OPT=""
BUILD=false
CACHE=false
RUN=false
SCREEN=false
EXEC=false
CONTAINER=""
DEVEL_DIR=""
LEARN_DIR=""
COMMAND=""
EXP=""
DETACH=false
SHM_SIZE="10g"
function print_help {
echo "Parameters:"
echo " IMAGE name \"$IMAGE_NAME\" [-n, --image-name]"
echo " IMAGE folder \"$IMAGE_FOLDER\" [-f, --image-folder]"
echo " GPU enabled ($GPU) [--no-gpu]"
echo " OPTIRUN disabled ($OPTIRUN) [--with-optirun]"
echo " BUILD: $BUILD [-b, --build], with CACHE: $CACHE [-c, --cache]"
echo " RUN: $RUN [-r, --run], with SCREEN: $SCREEN [-s, --screen]"
echo " EXEC: $EXEC [-e, --exec], CONTAINER: \"$CONTAINER\" (use docker ps for the id)"
echo " COMMAND: \"$COMMAND\" [--cmd]"
echo " EXP: \"$EXP\" [--exp]"
echo " DETACH: ($DETACH) [--detach]"
echo " DEVELOPMENT dir \"$DEVEL_DIR\" [-d, --devel]"
echo " LEARNING dir \"$LEARN_DIR\" [-l, --learn]"
echo " SHM_SIZE \"$SHM_SIZE\" [--shm-size]"
}
for arg in "$@"
do
case $arg in ## -l=*|--lib=*) DIR="${i#*=}" is the way to retrieve the parameter
-n=*|--image-name=*)
IMAGE_NAME="${arg#*=}"
;;
-f=*|--image-folder=*)
IMAGE_FOLDER="${arg#*=}"
;;
--no-gpu)
GPU=false
GPU_OPT=""
;;
--with-optirun)
OPTIRUN=true
OPTIRUN_OPT="optirun"
;;
--detach)
DETACH=true
;;
-b|--build)
BUILD=true
;;
-c|--cache) # it does nothing without BUILD=true
CACHE=true
;;
-r|--run)
RUN=true
;;
-s|--screen) # it does nothing without RUN=true
SCREEN=true
;;
-e=*|--exec=*) # it works only with RUN=false
EXEC=true
CONTAINER="${arg#*=}"
;;
--cmd=*)
COMMAND="${arg#*=}"
;;
--exp=*)
EXP="${arg#*=}"
;;
-d=*|--devel=*)
DEVEL_DIR="${arg#*=}"
;;
-l=*|--learn=*)
LEARN_DIR="${arg#*=}"
;;
--shm-size=*)
SHM_SIZE="${arg#*=}"
;;
*)
# unknown option
echo "Unknown option \"$arg\""
print_help
exit
;;
esac
done
print_help
# Tensorflow original image
# docker run -u $(id -u):$(id -g) --gpus all -it --rm tensorflow/tensorflow:latest-gpu-py3 bash
## Building the docker image
if [[ "$BUILD" = true ]]; then
if [[ "$CACHE" = true ]]; then
echo "Building the docker container using the cache, if present."
$OPTIRUN_OPT docker build \
--build-arg USER_ID=$(id -u ${USER}) \
--build-arg GROUP_ID=$(id -g ${USER}) \
-t "$IMAGE_NAME" "$IMAGE_FOLDER"
else
echo "Building the docker container ignoring the cache, even if present."
$OPTIRUN_OPT docker build \
--build-arg USER_ID=$(id -u ${USER}) \
--build-arg GROUP_ID=$(id -g ${USER}) \
--no-cache -t "$IMAGE_NAME" "$IMAGE_FOLDER"
fi
fi
if [[ "$RUN" = true ]]; then
# My docker build
MOUNT_DEVEL=""
if [[ $DEVEL_DIR ]]; then
MOUNT_DEVEL="--mount src=$DEVEL_DIR,target=/home/alice/devel,type=bind"
fi
MOUNT_LEARN=""
if [[ $LEARN_DIR ]]; then
MOUNT_LEARN="--mount src=$LEARN_DIR,target=/home/alice/learning,type=bind"
fi
CONT_NAME=""
if [[ $EXP ]]; then
CONT_NAME="--name $EXP"
fi
if [[ "$DETACH" = true ]]; then
DETACH="-d"
else
DETACH=""
fi
CURR_UID=$(id -u)
CURR_GID=$(id -g)
RUN_OPT="-u $CURR_UID:$CURR_GID --net=host --env DISPLAY=$DISPLAY \
--volume $XAUTHORITY:/home/alice/.Xauthority \
--volume /tmp/.X11-unix:/tmp/.X11-unix \
--privileged $MOUNT_DEVEL $MOUNT_LEARN \
--shm-size $SHM_SIZE $GPU_OPT $CONT_NAME \
-it $DETACH --rm $IMAGE_NAME:latest"
echo "$OPTIRUN_OPT docker run $RUN_OPT $COMMAND"
## Running docker
if [[ "$SCREEN" = true ]]; then
echo "Running the docker in a screen session."
screen -d -m \
$OPTIRUN_OPT docker run $RUN_OPT $COMMAND
else
$OPTIRUN_OPT docker run $RUN_OPT $COMMAND
fi
else
if [[ "$EXEC" = true ]]; then
echo "Attaching to a running docker (see container id using 'docker ps')."
$OPTIRUN_OPT docker exec -it "$CONTAINER" /bin/bash
fi
fi