Every so often you need to clean up after yourself and Docker. This could be as simple as removing unused containers or volumes. Either way, you need to know the commands necessary to make this happen. This is especially true when you’re running Docker on a virtual machine, where space is a premium.
The Docker cleanup tasks aren’t really all that complicated; they’re just easily forgotten. But when you have containers hanging around serving no purpose, you need to get rid of them (and all of their remnants), to free up space.
Let’s take care of just that.
1. Removing containers
Let’s face it, not every container you create continues to be used. It’s also very easy to forget you created those lingering containers. There are two commands you can run to stop and remove all containers. However, you might have containers that are in use, so you wouldn’t want to do a wholesale removal.
So to remove containers, you will want to first find out what containers you have on your system. To do this, issue the command:
docker ps -a
The above command will list out the containers, both running and not, on your system (Figure A).
Any container with a status of Exited is no longer running and can be safely deleted (if you have no plans on start it up again). To delete that exited container, issue the command:
docker rm CONTAINTER_ID
Where CONTAINER_ID is the first few digits of the actual ID associated with the container.
Say, however, you have running containers you no longer use. To delete them, you would first have to stop the container with the command:
docker stop CONTAINER_ID
Where CONTAINER_ID is the ID associated with the container. You can now delete the container in the same manner as described above.
Another way to remove all stopped (exited) containers is with the command:
docker rm -v $(docker ps -a -q -f status=exited)
2. Remove Docker volumes
A Docker data volume is a special directory, inside one or more containers, that is able to bypass Docker’s storage driver and can interact directly with the host file system. Data volumes serve as either persistent or shared data, between containers, and is initialized when a container is created. The problem is, when you delete a container, the volume may remain.
To remove (what is called) dangling volumes, you should issue the command:
docker volume rm $(docker volume ls -qf dangling=true)
You will see output similar to that shown in Figure B.
Finally, issue the following command to make sure the cleanup is complete:
docker volume ls -qf dangling=true | xargs -r docker volume rm
3. Use a minimal base image
Most official Docker images use the latest Debian as the base. Instead of working with a larger image (such as Ubuntu), you could always save space by starting with the official debian image (such as debian:jessie). If the Debian image is still too big, you can go even smaller by starting with the BusyBox image. You can start working with BusyBox by pulling the image, starting the container, and gaining access to the BusyBox shell with the command:
docker run -it --rm busybox
Once inside the BusyBox container, start building.
4. Make use of cron
Let’s say you use Docker with some regularity and never seem to find the time to clean up after yourself. If that’s you, there’s always cron at the ready. Let’s create a cron job that will do an automated, daily cleanup job.
The first thing to do is create the script for the job. Let’s name the script docker_clean.sh and we’ll save it in ~/.
The contents of the file will be:
âdocker rmi $(docker images -q -f dangling=true)
âdocker volume rm $(docker volume ls -qf dangling=true)
Save and close the file. In order for this to work, it must have the proper permissions. Issue the command:
chmod 755 ~/docker_clean.sh
For this to succeed, whatever user will be cronning the job, must be in the Docker group. To add that user, issue the command:
sudo usermod -aG docker USER
Where USER is the actual username.
That user will need to logout and log back in. Now we create the cron job by issuing the command (as the user in question):
At the bottom of the crontab file, add the following:
0 0 * * 1 ~/docker_clean.sh > /dev/null 2>&1
Save and close the crontab file. At this point, every midnight the Docker cleanup task will run and you’ll always enjoy a fresh and clean Docker experience.
Docker will serve you well. If you keep your server clean, there will be less of a chance it will run out of space–an issue that is especially important when Docker is running as a virtual machine. Keep Docker clean and you’ll enjoy a much-improved experience.