Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kulongwangzhi85/f6036857f9cd835d2daf745c04e6c2b1 to your computer and use it in GitHub Desktop.
Save kulongwangzhi85/f6036857f9cd835d2daf745c04e6c2b1 to your computer and use it in GitHub Desktop.
bash script to monitor and do something with docker events
#!/bin/sh
docker events --filter 'event=start' --filter 'event=stop' | while read event
do
container_id=`echo $event | sed 's/.*Z\ \(.*\):\ .*/\1/'`
echo $container_id
ipaddress=`docker inspect --format='{{.NetworkSettings.IPAddress}}' $container_id`
port=`docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' $container_id`
domain=`docker inspect --format='{{.Config.Domainname}}' $container_id`
host=`docker inspect --format='{{.Config.Hostname}}' $container_id`
echo $ipaddress
echo $port
echo $host.$domain
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment