Docker Tip #32: Automatically Clean Up after Docker Daily
Learn how to automatically remove dangling Docker images and other resources on a daily basis for Linux, Windows and MacOS.
The command we’re going to be executing is
docker system prune -f which will remove all stopped containers, all unused networks, all dangling images and build caches.
We’ll want to automatically execute this command every day at 3AM, but how we do it will depend on what OS you’re using. So here’s how to do it on all major operating systems.
We have access to the
cron utility, so this will be simple. Run
crontab -e, pick an editor (nano) if you haven’t done so already and then add this line to the bottom and save it:
0 3 * * * /usr/bin/docker system prune -f
Docker is most likely installed at
/usr/bin/docker but you can verify that by running
which docker. Change the path if you need to.
I am running WSL as my primary Linux development environment but when you close your terminal, the WSL process ends, so there’s no way to reasonably depend on
cron for WSL because you would need to keep your terminal open 24 / 7.
The steps below will work with or without WSL as long as you’re using Docker for Windows. If you are using Docker Toolbox you will need to change step 8 to launch the Docker Quickstart Terminal and pass in the Docker command to that as an argument.
Here’s how to do it using the built in Windows Task Scheduler:
- Search for “Computer Management” and run it
- Click “System Tools -> Task Scheduler” in the sidebar
- Click “Create Task” in the action bar on the right
- Name it “Docker-System-Prune”
- Change the security option to “Run whether user is logged on or not”
- Change “Configure for” (dropdown box) to “Windows 10”
- Goto the “Actions” tab and click “New”
- Enter in
C:\Program Files\Docker\Docker\Resources\bin\docker.exeas the Program/script
- Add the arguments
system prune -fand hit OK to anything it says
- Goto the “Triggers” tab and click “New”
- Change the settings to “Daily” and set the “Start” time to 3AM and recur every 1 day
- Click OK and then click OK again for Create Task
- Enter in your admin password
I don’t use a Mac but my buddy Scott does, so he provided me the steps listed below for LaunchD (a default scheduler on MacOS that is supposed to replace cron).
Create the LaunchD based schedule file:
sudo touch /Library/LaunchDaemons/DockerSystemPrune.plist
Add this content to the above file and save it:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>DockerSystemPrue</string> <key>Program</key> <string>/usr/local/bin/docker system prune -f</string> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>3</integer> <key>Minute</key> <integer>0</integer> </dict> </dict> </plist>
Load the task into LaunchD:
launchctl load -w /Library/LaunchDaemons/DockerSystemPrune.plist
At this point it’s activated and you’re good to go.