Update article

This commit is contained in:
Raven Scott 2024-09-18 19:35:49 -04:00
parent af22b29ec1
commit 30b02e6084

View File

@ -187,6 +187,48 @@ cmd(`sh /home/scripts/detectIP.sh ` + randNetwork).then(out => {
}); });
``` ```
## The detectIP script
```bash
#!/bin/bash
# Check if the network name is provided as a parameter
if [ $# -ne 1 ]; then
echo "Usage: $0 <network_name>"
exit 1
fi
# Extract the network name from the parameter
network_name="$1"
# Get the subnet of the Docker network
subnet=$(docker network inspect "$network_name" -f '{{(index .IPAM.Config 0).Subnet}}')
if [ -z "$subnet" ]; then
echo "Network '$network_name' not found or doesn't have an assigned subnet."
exit 1
fi
# Extract just the subnet prefix (e.g., 172.18.0)
subnet_prefix="${subnet%.*}"
# Loop through IP addresses in the subnet to find an available one
for ((i=2; i<=254; i++)); do
ip="${subnet_prefix}.$i"
# Check if the IP address is already in use by a running container
if ! ping -c 1 "$ip" &> /dev/null; then
echo "$ip"
exit 0
fi
done
echo "No available IP addresses found in the subnet of network '$network_name'."
exit 1
```
This script checks if exactly one argument (a network name) is provided, and if not, it displays usage instructions and exits. It retrieves the subnet of the specified Docker network, checks if the network exists and has an assigned subnet, and extracts the subnet prefix (e.g., 172.18.0). The script then loops through possible IP addresses within the subnet range, checking if each is available by attempting to ping it. If an available IP is found, it prints the IP and exits. If no available IP is found, it notifies the user and exits.
#### Operating System Selection #### Operating System Selection
The script supports multiple Linux distributions, such as Ubuntu, Debian, CentOS, and more. Depending on the command-line arguments passed, the script selects the appropriate operating system for the Docker container. Each OS is associated with specific resource settings (e.g., memory, CPUs) and restart policies, all configurable based on user input. The script supports multiple Linux distributions, such as Ubuntu, Debian, CentOS, and more. Depending on the command-line arguments passed, the script selects the appropriate operating system for the Docker container. Each OS is associated with specific resource settings (e.g., memory, CPUs) and restart policies, all configurable based on user input.