-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_docker.sh
More file actions
executable file
Β·160 lines (139 loc) Β· 4.59 KB
/
setup_docker.sh
File metadata and controls
executable file
Β·160 lines (139 loc) Β· 4.59 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
#!/bin/bash
RUN_CONTAINER=false
BUILD_CONTAINER=false
TAG_CONTAINER=false
PUSH_CONTAINER=false
CLEAN_CONTAINER=false
HOST=""
TAG="cockatoo_edge:latest" #set default tag in case user didnt provide one
validate_tag() {
if [[ ! "$1" =~ ^[a-zA-Z0-9._/-]+:[a-zA-Z0-9._-]+$ ]] && [[ ! "$1" =~ ^[a-zA-Z0-9._/-]+$ ]]; then
echo "[π] Error: Invalid tag format '$1'. Use format: name[:tag]. Example: cockatoo_edge:latest"
exit 1
fi
}
validate_host() {
if [[ ! "$1" =~ ^[a-zA-Z0-9.-]+:[0-9]+$ ]]; then
echo "[π] Error: Invalid host format '$1'. Use format: hostname:port or ip:port. Example: 192.168.1.1:5000"
exit 1
fi
}
while [[ $# -gt 0 ]]; do
case $1 in
-r|--run)
RUN_CONTAINER=true
shift
;;
-b|--build)
BUILD_CONTAINER=true
shift
;;
-t|--tag)
TAG_CONTAINER=true
if [[ $2 && $2 != -* ]]; then
validate_tag "$2"
TAG="$2"
shift 2
else
echo "[π] Warning: No tag name provided, using default $TAG"
shift
fi
;;
-p|--push)
PUSH_CONTAINER=true
shift
;;
-h|--host)
if [[ -z "$2" || "$2" == -* ]]; then
echo "[π] Error: --host requires a value"
exit 1
fi
validate_host "$2"
HOST="$2"
shift 2
;;
-c|--clean)
CLEAN_CONTAINER=true
docker image prune -a #docker will ask for confirmation, no need to handle it here
;;
*)
echo "[π] Unknown option $1"
echo "[π] Usage: $0 [-r|--run] [-b|--build] [-t|--tag [tag_name]] [-p|--push] [-h|--host <ip>:<port>] [-c|--clean]"
exit 1
;;
esac
done
if [ "$RUN_CONTAINER" = false ] && [ "$BUILD_CONTAINER" = false ] && [ "$TAG_CONTAINER" = false ] && [ "$PUSH_CONTAINER" = false ] && [ "$CLEAN_CONTAINER" = false ]; then
echo "[π] Error: Please provide at least one flag"
echo "[π] Usage: $0 [-r|--run] [-b|--build] [-t|--tag [tag_name]] [-p|--push] [-h|--host <ip>:<port>] [-c|--clean]"
exit 1
fi
if [ "$PUSH_CONTAINER" = true ]; then
if [ -z "$HOST" ]; then
echo "[π] Error: Host must be specified with -h or --host flag when using -p"
exit 1
fi
fi
if command -v docker >/dev/null 2>&1; then
echo "[π] Docker is already installed"
else
echo "[π] Docker could not be found, installing..."
if [ "$EUID" -ne 0 ]; then
echo "[π] Error: Docker installation requires root privileges. Please run with sudo."
exit 1
fi
if ! apt update; then
echo "[π] Error: Failed to update package list"
exit 1
fi
if ! apt install -y docker.io; then
echo "[π] Error: Failed to install Docker"
exit 1
fi
fi
if [ "$BUILD_CONTAINER" = true ]; then
if [ ! -f "Dockerfile" ]; then
echo "[π] Error: Dockerfile not found in current directory"
exit 1
fi
if ! docker build -t "$TAG" .; then
echo "[π] Error: Docker build failed"
exit 1
fi
fi
if [ "$TAG_CONTAINER" = true ] && [ "$BUILD_CONTAINER" = false ]; then
if ! docker image inspect "$TAG" >/dev/null 2>&1; then
echo "[π] Error: Image '$TAG' not found. Please build it first with -b flag."
exit 1
fi
fi
if [ "$PUSH_CONTAINER" = true ]; then
REMOTE_TAG="$HOST/$TAG"
if ! docker image inspect "$TAG" >/dev/null 2>&1; then
echo "[π] Error: Image '$TAG' not found. Please build it first with -b flag."
exit 1
fi
if [ "$TAG_CONTAINER" = false ]; then
echo "[π] Warning: Tagging container for remote registry without -t flag. Using default tag $TAG."
fi
echo "[π] Tagging container for remote registry as $REMOTE_TAG..."
if ! docker tag "$TAG" "$REMOTE_TAG"; then
echo "[π] Error: Failed to tag container for remote registry"
exit 1
fi
echo "[π] Pushing container to $HOST..."
if ! docker push "$REMOTE_TAG"; then
echo "[π] Error: Failed to push container to registry"
exit 1
fi
fi
if [ "$RUN_CONTAINER" = true ]; then
echo "[π] Running container..."
if ! docker run "$TAG"; then
echo "[π] Error: Failed to run container"
exit 1
fi
fi
if [ "$BUILD_CONTAINER" = true ] && [ "$RUN_CONTAINER" = false ]; then
echo "[π] Build complete. Use -r or --run flag to run the container."
fi