-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartdevctl
More file actions
executable file
·48 lines (35 loc) · 1.42 KB
/
startdevctl
File metadata and controls
executable file
·48 lines (35 loc) · 1.42 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
#!/bin/bash
container_name="$1"
additional_ports="$2"
image=$(docker inspect --format='{{.Config.Image}}' $container_name)
volume=$(docker inspect --format='{{range .Mounts}}{{.Source}}{{end}}' $container_name)
# Function to check if a port is in use
is_port_in_use() {
netstat -ltn | grep -q ":$1"
}
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
docker commit $container_name $image
docker rm $container_name
docker run --name $container_name $port_mappings -v "$volume:/home/coder" --privileged -d $image
# ports=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{if $conf}}{{(index $conf 0).HostPort}} {{end}}{{end}}' $container_name)