-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportdevctl
More file actions
executable file
·65 lines (52 loc) · 2.09 KB
/
Copy pathportdevctl
File metadata and controls
executable file
·65 lines (52 loc) · 2.09 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
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 <container_name> <technology> <volume> <additional_ports> <template_name>"
echo "Example: $0 my-container nodejs /path/to/volume 3001,3002 none"
echo " $0 my-container any /path/to/volume 4000 next-js"
exit 1
}
# Check if the correct number of arguments is provided
if [ "$#" -ne 5 ]; then
usage
fi
# Assign arguments to variables
container_name="$1"
technology="$2"
volume="$3"
additional_ports="$4"
template_name="$5"
# Generate image name
image_name="$container_name-$(head /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)"
# Build Docker image
docker buildx build --no-cache --build-arg ADDITIONAL_PACKAGES="$technology" --build-arg ADDITIONAL_PORT="$additional_ports" --build-arg TEMPLATE_NAME="$template_name" -f /usr/local/share/devbox/dockerfile /usr/local/share/devbox/ -t "$image_name"
# Function to check if a port is in use
is_port_in_use() {
netstat -ltn | grep -q ":$1"
}
# Find the first available port starting from 8080
port=8080
while is_port_in_use $port; do
port=$((port+1))
done
# Parse additional ports from the fourth argument
IFS=',' read -ra additional_ports_array <<< "$additional_ports"
for i in "${!additional_ports_array[@]}"; do
while is_port_in_use "${additional_ports_array[$i]}"; do
additional_ports_array[$i]=$((additional_ports_array[$i]+1))
done
done
# Join the available ports into a comma-separated string
ports_str="$port"
for additional_port in "${additional_ports_array[@]}"; do
ports_str="$ports_str,$additional_port"
done
echo "Launching $container_name on ports $ports_str and mounting $volume"
# Prepare -p options for Docker run
port_mappings="-p $port:8080"
for additional_port in "${additional_ports_array[@]}"; do
port_mappings="$port_mappings -p $additional_port:$additional_port"
done
# Run the Docker container with the available ports
docker run --name "$container_name" $port_mappings -v "$volume:/home/coder" --privileged -d "$image_name" "$template_name"
echo "Container $container_name started successfully with port mapping $port_mappings."