Skip to content

Instantly share code, notes, and snippets.

@czras
Created February 1, 2019 14:24
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 czras/3ce5d0bd123cd03dda76a0bae2850c2f to your computer and use it in GitHub Desktop.
Save czras/3ce5d0bd123cd03dda76a0bae2850c2f to your computer and use it in GitHub Desktop.
Register docker container hostnames in libvirt network.
#!/bin/sh
# adapted from https://gist.github.com/freshjones/b0713263033df8cc9f44
docker events --filter 'event=start' --filter 'event=die' | while read e
do
timestamp=`echo $e | cut -d " " -f 1`
object=`echo $e | cut -d " " -f 2`
event=`echo $e | cut -d " " -f 3`
container_id=`echo $e | cut -d " " -f 4`
echo $e
info=`docker inspect --format='{{.Name}} {{.Config.Hostname}} {{.NetworkSettings.IPAddress}}' $container_id`
name=`echo $info | cut -d " " -f 1 | cut -d "/" -f 2`
host=`echo $info | cut -d " " -f 2`
ipaddress=`echo $info | cut -d " " -f 3`
case $event in
start)
echo Registering container on libvirt network Name: $name DNS: $host IP: $ipaddress
sudo virsh net-update default add dns-host "<host ip='$ipaddress'><hostname>$host</hostname></host>" --live
;;
die)
echo Unregistering container on libvirt network Name: $name DNS: $host IP: $ipaddress
sudo virsh net-update default delete dns-host "<host><hostname>$host</hostname></host>" --live
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment