-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlucee
executable file
·212 lines (191 loc) · 5.7 KB
/
lucee
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
# Default values
default_image="lucee/lucee"
default_tag="5.4.7.1-SNAPSHOT"
default_port="3000"
volumes=()
# Function to display usage
usage() {
echo "
📘 Usage: $0 [options]
"
echo "Options:"
echo " 📦 -i, --image IMAGE Set the Docker image (default: $default_image)"
echo " 🏷️ -t, --tag TAG Set the Docker image tag (default: $default_tag)"
echo " 🚪 -p, --port PORT Set the host port (default: $default_port)"
echo " 📂 -v, --volume, -V PATTERN Add volume pattern (repeatable)"
echo " The volume pattern should be in the format [host_path]:[container_path]."
echo " Example: -v /path/on/host:/path/in/container"
echo " 🌱 -e, --env KEY=VALUE Set environment variable (repeatable)
Example: -e MY_VAR=my_value"
echo " 🔄 -d, --detach Run the container in detached mode"
echo " 📜 logs Show logs of the currently running container"
echo " ❌ stop Stop the currently running container"
echo " 📝 list List available image tags on Docker Hub"
echo " 🔍 dry-run Show what command would be executed without running it"
echo "
"
exit 1
}
# Parse arguments
detach=false
show_logs=false
stop_container=false
list_tags=false
dry_run=false
while [[ "$#" -gt 0 ]]; do
case $1 in
-e|--env)
env_vars+=("$2")
shift 2
;;
-i|--image)
image="$2"
shift 2
;;
-t|--tag)
tag="$2"
shift 2
;;
-p|--port)
port="$2"
shift 2
;;
-v|--volume|-V)
volumes+=("$2")
shift 2
;;
list)
list_tags=true
shift 1
;;
stop)
stop_container=true
shift 1
;;
logs)
show_logs=true
shift 1
;;
-d|--detach)
detach=true
shift 1
;;
dry-run)
dry_run=true
shift 1
;;
*)
usage
;;
esac
done
# Set defaults if not provided
current_path=$(pwd)
image=${image:-$default_image}
tag=${tag:-$default_tag}
port=${port:-$default_port}
container_name=$(basename "$current_path")
# Function to list available tags from Docker Hub
list_available_tags() {
echo "Fetching available tags for image: $default_image..."
local page=1
while true; do
response=$(curl -s "https://registry.hub.docker.com/v2/repositories/${default_image}/tags?page_size=100&page=${page}")
if [[ $? -ne 0 || -z "$response" ]]; then
echo "Error: Unable to fetch tags from Docker Hub. Please check your internet connection or the image name."
exit 1
fi
tags=$(echo "$response" | jq -r '.results[].name')
if [[ -z "$tags" ]]; then
echo "No more tags available."
break
fi
echo "$tags"
((page++))
read -p "Press [Enter] to see more tags or type 'q' to quit: " input
if [[ "$input" == "q" ]]; then
break
fi
done
}
# If list command is provided, list the tags and exit
if [[ $list_tags == true ]]; then
if ! command -v jq &> /dev/null; then
echo "jq is required for parsing JSON. Please install jq."
exit 1
fi
list_available_tags
exit 0
fi
# If logs command is provided, show logs of the running container and exit
if [[ $show_logs == true ]]; then
if docker ps --filter "name=^/${container_name}$" --format '{{.Names}}' | grep -q "${container_name}"; then
docker logs -f "$container_name"
else
echo "No container with the name '${container_name}' is currently running."
fi
exit 0
fi
# If stop command is provided, stop the running container and exit
if [[ $stop_container == true ]]; then
if docker ps --filter "name=^/${container_name}$" --format '{{.Names}}' | grep -q "${container_name}"; then
docker stop "$container_name"
echo "Container '${container_name}' has been stopped."
else
echo "No container with the name '${container_name}' is currently running."
fi
exit 0
fi
# Warn if a container with the same name is already running
if docker ps --filter "name=^/${container_name}$" --format '{{.Names}}' | grep -q "${container_name}"; then
echo "Warning: A container with the name '${container_name}' is already running. Please stop it before starting a new one."
exit 1
fi
# Default action is to run the Docker container
volume_args=""
env_args=""
for env_var in "${env_vars[@]}"; do
env_args+="-e ${env_var} "
done
for vol in "${volumes[@]}"; do
volume_args+="-v ${vol} "
done
find_available_port() {
local base_port=$1
local port=$base_port
while netstat -an | grep -q "\.$port "; do
((port++))
if (( port > base_port + 100 )); then
echo "Error: Could not find an available port within 100 attempts." >&2
exit 1
fi
done
echo $port
}
# Find an available port if the default is taken
port=$(find_available_port $port)
# Show settings before launching the container
cat <<EOM
🚀 Launching Docker Container with the following settings:
📦 Container Name: $container_name
📦 Image: $image
🏷️ Tag: $tag
🚪 Port: $port
📂 Volumes:
📂 $current_path:/app
$(for vol in "${volumes[@]}"; do echo " 📂 $vol"; done)
🌱 Environment Variables:
$(for env_var in "${env_vars[@]}"; do echo " 🌱 $env_var"; done)
EOM
# Run the Docker container
detach_flag=""
if [[ $detach == true ]]; then
detach_flag="-d"
fi
if [[ $dry_run == true ]]; then
echo "Dry run mode enabled. The following command would be executed:"
echo "docker run --rm $detach_flag --name "$container_name" -v "$current_path":/app -p "$port":8080 $volume_args $env_args "$image":"$tag""
exit 0
fi
docker run --rm $detach_flag --name "$container_name" -v "$current_path":/app -p "$port":8080 $volume_args $env_args "$image":"$tag"