Created
February 1, 2019 14:24
-
-
Save czras/3ce5d0bd123cd03dda76a0bae2850c2f to your computer and use it in GitHub Desktop.
Register docker container hostnames in libvirt network.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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