|
| 1 | +# Docker Cleanup for AdCP Tests |
| 2 | + |
| 3 | +## The Problem |
| 4 | + |
| 5 | +Running AdCP tests can accumulate Docker resources over time: |
| 6 | + |
| 7 | +1. **E2E tests** create full Docker Compose stacks (~1.5GB per run) |
| 8 | +2. **Conductor workspaces** create unique Docker images per workspace (`amman-adcp-server`, `monaco-adcp-server`, etc.) |
| 9 | +3. **Integration tests** create per-test PostgreSQL databases (cleaned up in Python but leave Docker state) |
| 10 | +4. **Dangling volumes** accumulate from incomplete test runs |
| 11 | + |
| 12 | +**Impact**: 100GB+ of Docker resources can accumulate over weeks of development. |
| 13 | + |
| 14 | +## The Solution |
| 15 | + |
| 16 | +### Automatic Cleanup (Built-in) |
| 17 | + |
| 18 | +**✅ Fixed in this PR:** |
| 19 | +- E2E tests now clean up Docker resources automatically after each test session |
| 20 | +- `run_all_tests.sh` prunes dangling volumes on teardown |
| 21 | +- Only test containers are affected - your local dev environment is safe |
| 22 | + |
| 23 | +### Manual Cleanup Script |
| 24 | + |
| 25 | +Run the cleanup script periodically: |
| 26 | + |
| 27 | +```bash |
| 28 | +./scripts/cleanup_docker.sh |
| 29 | +``` |
| 30 | + |
| 31 | +This removes: |
| 32 | +- ✅ Stopped test containers (`adcp-test-*`) |
| 33 | +- ✅ Dangling/unused volumes |
| 34 | +- ✅ Old workspace images (keeps 2 most recent) |
| 35 | +- ✅ Dangling intermediate images |
| 36 | + |
| 37 | +**Safe to run**: Your local dev environment (`docker-compose up`) is not affected. |
| 38 | + |
| 39 | +### What Gets Cleaned |
| 40 | + |
| 41 | +| Resource Type | Pattern | Cleanup Method | Safe? | |
| 42 | +|--------------|---------|----------------|-------| |
| 43 | +| Test containers | `adcp-test-*` | `docker rm -f` | ✅ Yes | |
| 44 | +| Test volumes | Dangling/unused | `docker volume prune -f` | ✅ Yes | |
| 45 | +| Workspace images | `*-adcp-server` | Keep 2 newest, remove rest | ✅ Yes | |
| 46 | +| Cache volumes | `adcp_global_*_cache` | Preserved (labeled) | ✅ Never removed | |
| 47 | +| Dev containers | `docker-compose.yml` | Not touched | ✅ Never removed | |
| 48 | + |
| 49 | +### Best Practices |
| 50 | + |
| 51 | +**1. Prefer Quick Mode for Iteration** |
| 52 | +```bash |
| 53 | +./run_all_tests.sh quick # No Docker (fast, ~1 min) |
| 54 | +``` |
| 55 | + |
| 56 | +**2. Use CI Mode Sparingly** |
| 57 | +```bash |
| 58 | +./run_all_tests.sh ci # Full Docker stack (~5 min) |
| 59 | +``` |
| 60 | + |
| 61 | +**3. Run Cleanup Weekly** |
| 62 | +```bash |
| 63 | +./scripts/cleanup_docker.sh |
| 64 | +``` |
| 65 | + |
| 66 | +**4. Check Docker Usage** |
| 67 | +```bash |
| 68 | +docker system df # See what's using space |
| 69 | +``` |
| 70 | + |
| 71 | +### Understanding Docker Usage |
| 72 | + |
| 73 | +```bash |
| 74 | +# See all AdCP-related images |
| 75 | +docker images --filter "reference=*adcp*" |
| 76 | + |
| 77 | +# See all test containers (running and stopped) |
| 78 | +docker ps -a --filter "name=adcp-test-" |
| 79 | + |
| 80 | +# See all volumes |
| 81 | +docker volume ls --filter "name=adcp" |
| 82 | + |
| 83 | +# Total Docker disk usage |
| 84 | +docker system df |
| 85 | +``` |
| 86 | + |
| 87 | +### When to Run Cleanup |
| 88 | + |
| 89 | +**Weekly maintenance:** |
| 90 | +- After heavy development sessions |
| 91 | +- Before/after working on different Conductor workspaces |
| 92 | +- When Docker disk usage is high (`docker system df`) |
| 93 | + |
| 94 | +**Symptoms of accumulation:** |
| 95 | +- Docker using 50GB+ (`docker system df`) |
| 96 | +- Slow Docker operations |
| 97 | +- "No space left on device" errors |
| 98 | + |
| 99 | +## Technical Details |
| 100 | + |
| 101 | +### Why This Happened |
| 102 | + |
| 103 | +1. **E2E tests (`tests/e2e/conftest.py`):** |
| 104 | + - Cleanup code at end of `docker_services_e2e` was commented out |
| 105 | + - Each E2E test run left containers and volumes behind |
| 106 | + - **Fixed**: Added proper cleanup in fixture teardown |
| 107 | + |
| 108 | +2. **Conductor workspaces:** |
| 109 | + - Each workspace (`amman`, `monaco`, etc.) builds its own Docker image |
| 110 | + - Images are 1.4-1.7GB each |
| 111 | + - **Fixed**: Cleanup script removes old workspace images (keeps 2 most recent) |
| 112 | + |
| 113 | +3. **Integration tests:** |
| 114 | + - Create unique PostgreSQL databases (`test_a3f8d92c`) per test |
| 115 | + - Databases cleaned in Python, but Docker metadata accumulates |
| 116 | + - **Fixed**: Added `docker volume prune` to teardown |
| 117 | + |
| 118 | +### Implementation |
| 119 | + |
| 120 | +**E2E Test Cleanup (`tests/e2e/conftest.py`):** |
| 121 | +```python |
| 122 | +# After yield in docker_services_e2e fixture |
| 123 | +if not use_existing_services: |
| 124 | + print("\n🧹 Cleaning up Docker resources...") |
| 125 | + subprocess.run(["docker-compose", "down", "-v"], capture_output=True) |
| 126 | + subprocess.run(["docker", "volume", "prune", "-f"], capture_output=True) |
| 127 | +``` |
| 128 | + |
| 129 | +**Test Runner Cleanup (`run_all_tests.sh`):** |
| 130 | +```bash |
| 131 | +teardown_docker_stack() { |
| 132 | + docker-compose -p "$COMPOSE_PROJECT_NAME" down -v |
| 133 | + docker volume prune -f --filter "label!=preserve" |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +**Manual Cleanup Script (`scripts/cleanup_docker.sh`):** |
| 138 | +- Removes test containers by name pattern |
| 139 | +- Prunes dangling volumes (except labeled as `preserve`) |
| 140 | +- Removes old workspace images (keeps 2 most recent) |
| 141 | +- Shows before/after disk usage |
| 142 | + |
| 143 | +### Prevention |
| 144 | + |
| 145 | +**1. Quick mode for development:** |
| 146 | +Uses integration_db fixture with real PostgreSQL but no Docker Compose overhead. |
| 147 | + |
| 148 | +**2. CI mode only when necessary:** |
| 149 | +Full Docker stack is needed for E2E tests but not for rapid iteration. |
| 150 | + |
| 151 | +**3. Automatic cleanup:** |
| 152 | +Tests now clean up after themselves - no manual intervention needed. |
| 153 | + |
| 154 | +**4. Periodic maintenance:** |
| 155 | +Run `./scripts/cleanup_docker.sh` weekly to remove orphaned resources. |
| 156 | + |
| 157 | +## References |
| 158 | + |
| 159 | +- Issue: 100GB of Docker resources accumulated from test runs |
| 160 | +- Root cause: E2E test cleanup was disabled, workspace images accumulated |
| 161 | +- Fix: [This PR] Re-enabled cleanup + added maintenance script |
| 162 | +- Prevention: Use quick mode for iteration, CI mode for validation |
0 commit comments