Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jaygaha/e0b3025f4e7567f4a637f65971a02584 to your computer and use it in GitHub Desktop.

Select an option

Save jaygaha/e0b3025f4e7567f4a637f65971a02584 to your computer and use it in GitHub Desktop.
Fix for Docker Desktop disk bloat (10GB+) caused by Laravel, Lumen/PHPUnit test suites. Includes instructions for migrating /tmp to tmpfs (RAM) for faster, self-cleaning unit tests.

Docker Optimization: Fixing disk bloating from unit tests

If your Docker Desktop Docker.raw file is exploding in size (10GB+), and you run frequent unit tests in a Lumen/Laravel environment, the culprit is likely orphaned files in /tmp.

This guide shows how to identify the bloat and move temporary test data to RAM for a faster, self-cleaning setup.

1. Identify the culprit

When Docker Desktop shows high disk usage, first check where the data is sitting:

# Check high-level breakdown
docker system df -v

# Check specific container internal usage
docker exec -it <container_name> du -sh /* | sort -h

Commonly, /tmp or /var/log are the main offenders in PHP environments.

2. Immediate cleanup

To instantly recover space without restarting everything, wipe the internal temporary files and build cache:

# Wipe container's temp folder
docker exec -it <container_name> sh -c "rm -rf /tmp/*"

# Reclaim space from old builds
docker builder prune -f

3. Permanent solution: move /tmp to RAM

Instead of writing temporary test data to your MacBook's SSD, mount /tmp as a tmpfs (RAM-based filesystem). This makes tests faster and auto-cleans everything on restart.

Pro

  • Tests run significantly faster.
  • Data is wiped instantly when the container stops.
  • Saves your SSD from unnecessary wear.

Update your docker-compose.yml:

services:
  your-api-service:
    # ...
    tmpfs:
      # Mounts /tmp in RAM. 
      # 'exec' allows PHP script execution; 'mode=777' ensures write access.
      - /tmp:exec,mode=777

4. Framework tweaks (Laravel, Lumen/PHPUnit)

Ensure your testing environment doesn't force persistent file writes by updating phpunit.xml:

  • Change CACHE_DRIVER to array.
  • Change SESSION_DRIVER to array.

5. Apply and Verify

Restart your containers to activate the RAM disk:

docker-compose down && docker-compose up -d

Verify the mount is working:

docker exec -it <container_name> df -h /tmp
# Should show "Mounted on: /tmp" with type "tmpfs"

Tip: If you still see high host-disk usage on your Mac, go to Docker Settings > Resources and click Apply & Restart to trigger the virtual disk's internal trimming process.

6. Reclaim build cache

If you still have several GBs of "Reclaimable" space in docker system df, clear your old build layers:

docker builder prune -f

Note: If your tests start failing with "Out of Memory," simply increase the Memory slider in Docker Desktop > Settings > Resources.

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