Skip to content

Instantly share code, notes, and snippets.

@robsonke
Last active February 13, 2026 00:44
Show Gist options
  • Select an option

  • Save robsonke/c5c478bae476adb32d48 to your computer and use it in GitHub Desktop.

Select an option

Save robsonke/c5c478bae476adb32d48 to your computer and use it in GitHub Desktop.
This Bash script will loop through all running docker containers on a host and list the disk usage per mount. In case it's breaching the 65%, it will email you.
#!/bin/bash
# get all running docker container names
containers=$(sudo docker ps | awk '{if(NR>1) print $NF}')
host=$(hostname)
# loop through all containers
for container in $containers
do
echo "Container: $container"
percentages=($(sudo docker exec $container /bin/sh -c "df -h | grep -vE '^Filesystem|shm|boot' | awk '{ print +\$5 }'"))
mounts=($(sudo docker exec $container /bin/sh -c "df -h | grep -vE '^Filesystem|shm|boot' | awk '{ print \$6 }'"))
for index in ${!mounts[*]}; do
echo "Mount ${mounts[index]}: ${percentages[index]}%"
if (( ${percentages[index]} > 70 )); then
message="[ERROR] At $host and Docker container $container the mount ${mounts[index]} is at ${percentages[index]}% of its disk space. Please check this."
echo $message
echo $message | mail -s "Docker container $container at $host is out of disk space" "r.sonke@maxxton.com"
fi
done
echo ================================
done
@qwertycody

Copy link
Copy Markdown

loved this, thank you.

@famuyiwadayo

Copy link
Copy Markdown

This is so sweet, Thank you

@cristianghita24

Copy link
Copy Markdown

This helped me a lot, thank you

ghost commented Feb 22, 2021

Copy link
Copy Markdown

This helped me, thank you.

@shivanimmagadda

shivanimmagadda commented Jun 10, 2021

Copy link
Copy Markdown

I am looking for a bash script which list the images which are tagged with another version of image. any example?.

@JunglistHyperD

Copy link
Copy Markdown

Smoove! 👍 thanks

@PapsOu

PapsOu commented Aug 2, 2021

Copy link
Copy Markdown

sudo docker ps ? sudo ? You should configure correctly your docker environment...

@imartinflores

imartinflores commented Sep 24, 2021

Copy link
Copy Markdown

Is there any way to add each container to an AssociativeArray , seems like it is not looping through them ,
but just returning the list of them as only 1 item. i.e

for container in $containers
do
myArray+=([key]=$container)
done
echo "${#myArray[@]}"

Is just printing the first container

Am I doing something wrong in there ?
Thanks!

@cottton

cottton commented May 6, 2022

Copy link
Copy Markdown

Should disable this line in the example or at least make the email an example email

echo $message | mail -s "Docker container $container at $host is out of disk space" "r.sonke@maxxton.com"

@robsonke

robsonke commented May 6, 2022

Copy link
Copy Markdown
Author

Should disable this line in the example or at least make the email an example email

echo $message | mail -s "Docker container $container at $host is out of disk space" "r.sonke@maxxton.com"

Oh haha yes, pretty old script. People even copy pasted it without even modifying and sharing me their monitoring emails....

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