Skip to content

Instantly share code, notes, and snippets.

@freshjones
Created February 5, 2015 23:18
Show Gist options
  • Save freshjones/b0713263033df8cc9f44 to your computer and use it in GitHub Desktop.
Save freshjones/b0713263033df8cc9f44 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
@phedders
Copy link

there is also the python docker event service... very nice

@gbrian
Copy link

gbrian commented Jun 7, 2021

Many thanks

My version inspired by yours.

#!/bin/sh 

event () {
    timestamp=$1
    event_type=$3
    container_id=$4
    name=$(docker inspect --format {{.Name}} $container_id)
    echo "$timestamp: $event_type $container_id $name"
}

docker events --filter 'event=create' --filter 'event=destroy' | while read event
do
    event $event
done;

# TEST
# event 2021-06-07T15:37:06.964878457+02:00 container create 83633249913cfed271fde94c29f198e05468ea4b6eb5f0d068f8244f63e96d44 (image=.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment