-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
executable file
·126 lines (109 loc) · 2.81 KB
/
startup.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
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
#!/bin/bash
IMAGE_NAME=ubuntu-custom
CONTAINER_NAME=myubuntu
USER_NAME=myubuntu
USER_PASSWORD=myubuntu
ALIAS=ubuntu
DIR=$(pwd)
NC='\033[0m'
GREEN='\033[0;32m'
RED='\033[0;31m'
define() { IFS=$'\n' read -r -d '' "${1}" || true; }
description="Run a custom ubuntu container"
usage_text=""
define usage_text <<'EOF'
USAGE:
./cs2110docker.sh [start|delete|-h|--help]
OPTIONS:
start
Start a new container. This is the default if no options
are provided.
stop
Stop a running container.
delete
Stop and delete any existing container.
-h, --help
Show this help text.
EOF
print_help() {
>&2 echo -e "$description\n\n$usage_text"
}
print_usage() {
>&2 echo "$usage_text"
}
action=""
if [ $# -eq 0 ]; then
action="start"
elif [ $# -eq 1 ]; then
case "$1" in
start)
action="start"
;;
stop)
action="stop"
;;
delete)
action="delete"
;;
-h|--help)
print_help
exit 0
;;
*)
>&2 echo "Error: unrecognized argument: $1"
>&2 echo ""
print_usage
exit 1
;;
esac
elif [ $# -gt 1 ]; then
>&2 echo "Error: too many arguments"
>&2 echo ""
print_usage
exit 1
fi
# Stop container
if [ "$action" = "stop" ]; then
docker stop ${CONTAINER_NAME}
echo -e "${GREEN}Successfully stopped $CONTAINER_NAME${NC}"
exit 0
fi
# Stop and delete container
if [ "$action" = "delete" ]; then
docker stop ${CONTAINER_NAME} && docker rm -f ${CONTAINER_NAME}
echo -e "${GREEN}Successfully deleted $CONTAINER_NAME${NC}"
exit 0
fi
# Check for Docker
if ! docker -v >/dev/null; then
>&2 echo "ERROR: Docker not found. Please install Docker before running this script."
exit 1
fi
if ! docker container ls >/dev/null; then
>&2 echo "ERROR: Docker is not currently running. Please start Docker before running this script."
exit 1
fi
# Add alias to .zshrc and set DIR
if [[ $(grep "alias $ALIAS" ~/.zshrc 2> /dev/null) == "" ]]; then
echo alias $ALIAS="$DIR/startup.sh" >> ~/.zshrc
else
DIR=$(grep "alias $ALIAS" ~/.zshrc)
DIR=${DIR#*=}
DIR=${DIR%/startup.sh}
fi
# Build image
docker build -t ${IMAGE_NAME} --build-arg USER_NAME="$USER_NAME" --build-arg USER_PASSWORD="$USER_PASSWORD" "$DIR"
# Create container if it doesn't exist
# Mounts current directory to docker container at /home/$USER_NAME/host/
{
if [ ! "$(docker ps -a -q -f name=${CONTAINER_NAME})" ]; then
echo -e "${GREEN}Creating docker container...${NC}"
docker run -v "$DIR:/home/$USER_NAME/host/" -dit --platform linux/amd64 --name ${CONTAINER_NAME} ${IMAGE_NAME} > /dev/null 2>&1
fi
} || {
echo -e "${RED}Failed to create container. Exiting script.${NC}"
exit 1
}
# Start container and attach to terminal
echo -e "${GREEN}Starting container...${NC}"
docker start ${CONTAINER_NAME} && docker attach ${CONTAINER_NAME}