6.2.3.1 One Off Script Execution
A focused guide to One Off Script Execution, connecting core concepts with practical Docker and container operations.
One-off script execution uses a container purely as an isolated, disposable environment for running a specific script once, taking advantage of the image's already-configured environment and dependencies without needing to maintain a separate, persistent service for that purpose.
Running a Script Directly
A script can be executed directly as the container's command, using whatever runtime and dependencies the image already provides.
docker run --rm myapp:1.0 python scripts/cleanup_old_records.py
This runs a specific maintenance script using the application image's existing Python environment and dependencies, without needing a separate, dedicated image just for this script.
Passing Arguments to a One-Off Script
Arguments can be supplied directly as part of the container's command, customizing the script's specific behavior for this particular invocation.
docker run --rm myapp:1.0 python scripts/export_data.py --format=csv --since=2026-01-01
Scheduling Regular One-Off Script Execution
A script intended to run on a recurring schedule (rather than as a continuously running service) can be triggered repeatedly through external scheduling, with each invocation creating and disposing of its own fresh container.
0 2 * * * docker run --rm myapp:1.0 python scripts/nightly_report.py
A cron entry like this runs the script as a fresh, isolated one-off container every night, without needing a persistent container running continuously just waiting for this scheduled time.
Why This Approach Suits Many Maintenance Tasks
Using one-off containers for scheduled or occasional script execution avoids the overhead and complexity of keeping a dedicated service running purely to occasionally perform this task, while still benefiting from the same consistent, isolated environment the main application uses.
docker run --rm myapp:1.0 python scripts/health_audit.py
Why One-Off Script Execution Matters
This pattern provides a lightweight, consistent way to run maintenance, reporting, or administrative scripts using an application's existing, well-tested environment, without the overhead of maintaining a separate persistent service solely for this occasional purpose.