✦ For everyone, free.

Practical knowledge for real and everyday life

Home

6.2.3.4 Cleanup Task Containers

A focused guide to Cleanup Task Containers, connecting core concepts with practical Docker and container operations.

Cleanup task containers run a one-off maintenance operation — purging old data, removing stale temporary files, reclaiming unused resources — as a discrete, isolated container task, often scheduled to run periodically without requiring a continuously running service dedicated to this purpose.

Running a Cleanup Task

A cleanup script runs using the application's existing image and configuration, performing its specific maintenance task and exiting once complete.

docker run --rm -e DATABASE_URL=$DATABASE_URL myapp:1.0 python scripts/purge_expired_sessions.py
Scheduling Periodic Cleanup

A cleanup task intended to run regularly is typically triggered through external scheduling, with each scheduled run creating its own fresh, isolated container.

0 3 * * * docker run --rm myapp:1.0 python scripts/cleanup_old_logs.py
Why Cleanup Tasks Suit the One-Off Container Pattern Well

A cleanup task typically runs briefly and infrequently, making the overhead of a continuously running dedicated service unjustified — creating a fresh container only when the cleanup actually needs to run is a more efficient use of resources.

docker run --rm myapp:1.0 python scripts/vacuum_database.py
Logging and Monitoring Cleanup Task Results

Even though a cleanup container is removed after completing, capturing its output and exit code before removal provides visibility into whether the cleanup actually succeeded.

docker run --rm myapp:1.0 python scripts/cleanup_old_logs.py >> /var/log/cleanup.log 2>&1
echo "Cleanup exit code: $?" >> /var/log/cleanup.log
Why Cleanup Task Containers Matter

Using disposable, one-off containers for periodic maintenance tasks provides a lightweight, resource-efficient pattern that avoids the overhead of a dedicated, continuously running service while still ensuring these necessary housekeeping tasks happen reliably on their intended schedule.