-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_docker.sh
executable file
·77 lines (62 loc) · 2.06 KB
/
run_docker.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
#!/bin/bash
#
# Docker build image, run container, execute last container.
#
# - Author: Jongkuk Lim
# - Contact: [email protected]
xhost +
ORG=jmarpledev
PRJ_NAME=${PWD##*/}
PRJ_NAME="$(tr [A-Z] [a-z] <<< "$PRJ_NAME")"
DOCKER_TAG=$ORG/$PRJ_NAME
CMD_ARGS=( ${@} )
CMD_ARGS=${CMD_ARGS[*]:1}
RUN_SHELL=/usr/bin/zsh
if [[ $2 == :* ]]; then
DOCKER_TAG=$DOCKER_TAG$2
CMD_ARGS=${CMD_ARGS[*]:2}
elif [ "$2" = "bash" ]; then
RUN_SHELL=/bin/bash
$RUN_SHELL CMD_ARGS=${CMD_ARGS[*]:2}
fi
if [ "$ARCH" = "aarch64" ]; then
DOCKER_FILE=./docker/Dockerfile.aarch64
else
DOCKER_FILE=./docker/Dockerfile
fi
if [ "$1" = "build" ]; then
if [ "$RUN_SHELL" = "/bin/bash" ]; then
CMD_ARGS="$CMD_ARGS --build-arg USE_ZSH=false"
fi
echo "Building a docker image with tagname $DOCKER_TAG and arguments $CMD_ARGS"
docker build . -t $DOCKER_TAG -f $DOCKER_FILE $CMD_ARGS --build-arg UID=`id -u` --build-arg GID=`id -g`
elif [ "$1" = "run" ]; then
if test -f "$HOME/.gitconfig"; then
CMD_ARGS="$CMD_ARGS -v $HOME/.gitconfig:/home/user/.gitconfig"
fi
echo "Run a docker image with tagname $DOCKER_TAG and arguments $CMD_ARGS"
docker run -tid --privileged --gpus all \
-e DISPLAY=${DISPLAY} \
-e TERM=xterm-256color \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
-v /dev:/dev \
-v $PWD:/home/user/$PRJ_NAME \
--network host \
$CMD_ARGS \
$DOCKER_TAG $RUN_SHELL
last_cont_id=$(docker ps -qn 1)
echo $(docker ps -qn 1) > $PWD/.last_exec_cont_id.txt
docker exec -ti $last_cont_id $RUN_SHELL
elif [ "$1" = "exec" ]; then
echo "Execute the last docker container"
last_cont_id=$(tail -1 $PWD/.last_exec_cont_id.txt)
docker start ${last_cont_id}
docker exec -ti ${last_cont_id} $RUN_SHELL
else
echo ""
echo "============= $0 [Usages] ============"
echo "1) $0 build : build docker image"
echo " build --no-cache : Build docker image without cache"
echo "2) $0 run : launch a new docker container"
echo "3) $0 exec : execute last container launched"
fi