Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@CpuID

This comment has been minimized.

Copy link

@CpuID CpuID commented Jan 4, 2019

nice one

@phedders

This comment has been minimized.

Copy link

@phedders phedders commented Nov 23, 2019

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

@gbrian

This comment has been minimized.

Copy link

@gbrian 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