Introduction
Over time, Terraform Enterprise operations can leave behind unused Docker containers, volumes, and images. These dangling resources consume disk space and can degrade OverlayFS performance. This guide provides a procedure to automate the cleanup of these resources using a daily cron job.
Expected Outcome
This procedure configures a daily cron job to automatically prune dangling Docker containers, volumes, and images, which helps maintain healthy OverlayFS performance.
Prerequisites
- A Linux-based Terraform Enterprise installation.
- The
crondservice must be enabled. - You must have root access to the Terraform Enterprise host.
Procedure
Note: If you use a custom worker or agent image, you should omit the
docker image prune -fcommand from the script to avoid having to re-pull the image manually.
-
Create the daily cron script.
Log into the Terraform Enterprise host via SSH and switch to the
rootuser. Run the following command to create the/etc/cron.daily/docker-prunescript.$ cat > /etc/cron.daily/docker-prune << EOF #!/bin/bash echo "Pruning dangling Docker containers" docker container prune -f echo "Pruning dangling Docker volumes" docker volume prune -f echo "Pruning dangling Docker images" docker image prune -f EOF
-
Make the script executable.
Run the
chmodcommand to set the executable permission on the script.$ chmod +x /etc/cron.daily/docker-prune
-
Verify the script.
Manually execute the script to confirm it runs as expected.
$ run-parts /etc/cron.daily
The command should produce output similar to the following.
/etc/cron.daily/docker-prune: Pruning dangling Docker containers Total reclaimed space: 0B Pruning dangling Docker volumes Total reclaimed space: 0B Pruning dangling Docker images Total reclaimed space: 0B