Skip to content

Instantly share code, notes, and snippets.

@faiyazalam
Last active November 18, 2023 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faiyazalam/8f500b7305f00211960c513b6247667e to your computer and use it in GitHub Desktop.
Save faiyazalam/8f500b7305f00211960c513b6247667e to your computer and use it in GitHub Desktop.
Docker Hosts Update Script Using Container Names
Docker Hosts Update Script
This shell script updates the `/etc/hosts` file on a Docker host machine by mapping container IP addresses to specified hostnames or container names.
Author:
https://github.com/faiyazalam
Parameters:
--containers: Comma-separated list of Docker container names.
--urls: Comma-separated list of corresponding hostnames. If not provided, defaults to container names.
Behavior:
Parses provided container names and URLs to update the /etc/hosts file.
Retrieves container IP addresses using Docker commands.
Checks for existing hostname entries and updates or appends to /etc/hosts.
Example usage:
bash script.sh --containers="container_name1,container_name2" --urls="example1.local,example2.local"
Note:
Ensure the script is run with appropriate permissions to modify the /etc/hosts file.
Container names and hostnames must correspond in order.
# Docker Hosts Update Script Using Container Names
## Description: This shell script updates the `/etc/hosts` file on a Docker host machine by mapping container IP addresses to specified hostnames or container names.
## Usage: bash script.sh --containers="container_name1,container_name2" --urls="hostname1,hostname2"
#Author: https://github.com/faiyazalam
#!/bin/bash
containers=""
urls=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--containers=*)
containers="${1#*=}"
shift
;;
--urls=*)
urls="${1#*=}"
shift
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
done
# Convert comma-separated values to arrays
IFS=',' read -r -a containers_array <<< "$containers"
# If URLs are not provided, set them to the same as containers
if [ -z "$urls" ]; then
urls="${containers}"
fi
IFS=',' read -r -a urls_array <<< "$urls"
# Check if the number of containers and URLs match
if [ ${#containers_array[@]} -ne ${#urls_array[@]} ]; then
echo "The number of containers and URLs should match."
exit 1
fi
# Loop through arrays to update /etc/hosts
for ((i=0; i<${#containers_array[@]}; i++)); do
container_id=$(docker ps -aqf "name=${containers_array[i]}")
# Check if the container ID exists
if [ -z "$container_id" ]; then
echo "Container ${containers_array[i]} not found."
exit 1
fi
ip_address=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$container_id")
hostname="${urls_array[i]}"
# Check if the entry exists in /etc/hosts
if grep -q "$hostname" /etc/hosts; then
sudo sed -i "s/.*$hostname.*/$ip_address $hostname/" /etc/hosts
echo "$hostname entry updated in /etc/hosts"
else
# Add the entry to /etc/hosts
echo "$ip_address $hostname" | sudo tee -a /etc/hosts
echo "$hostname entry added to /etc/hosts"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment