Skip to content

Instantly share code, notes, and snippets.

@stanislavb
Created April 12, 2016 11:12
Show Gist options
  • Save stanislavb/6634fc35b3d1655201a93d2dd2c3a366 to your computer and use it in GitHub Desktop.
Save stanislavb/6634fc35b3d1655201a93d2dd2c3a366 to your computer and use it in GitHub Desktop.
Docker clean-up script for cron
#!/bin/bash
# Do not run if removal already in progress.
pgrep "docker rm" && exit 0
# Remove Dead and Exited containers.
docker rm $(docker ps -a | grep "Dead\|Exited" | awk '{print $1}'); true
# It will fail to remove images currently in use.
docker rmi $(docker images -qf dangling=true); true
# Clean up unused docker volumes
docker volume rm $(docker volume ls -qf dangling=true); true
@stakka
Copy link

stakka commented Jul 9, 2021

pipe to xargs using --no-run-if-empty to avoid the docker command complaining about empty input.
your last line would become:

docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm

@turric4n
Copy link

turric4n commented Oct 19, 2022

#!/bin/bash

# Do not run if removal already in progress.
pgrep "docker rm" && exit 0

# Remove Dead and Exited containers.
docker rm $(docker ps -a | grep "Dead\|Exited" | awk '{print $1}'); true

# It will fail to remove images currently in use.
docker rmi $(docker images -qf dangling=true); true

# Clean up unused docker volumes
docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm

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