6.2.3 One Off Containers
A focused guide to One Off Containers, connecting core concepts with practical Docker and container operations.
One-off containers are containers created to perform a single, specific, finite task — running a script, executing a command, performing a quick test — rather than running as a continuously operating service, typically combined with --rm to avoid leaving behind an unneeded, stopped container afterward.
Characteristics of a One-Off Container
A one-off container's main process is expected to complete and exit naturally once its specific task is done, in contrast to a service container's main process, which is expected to keep running indefinitely.
docker run --rm myapp:1.0 python manage.py migrate
This container performs a single database migration task, exits once complete, and is automatically removed.
Why --rm Is a Natural Fit for One-Off Containers
Since a one-off container's writable layer rarely needs to be inspected or reused after its task completes, automatically removing it avoids unnecessary accumulation of stopped containers serving no further purpose.
docker run --rm myapp:1.0 some-validation-script.sh
Capturing Output From a One-Off Container
Even though the container itself is removed, its output can still be captured before it disappears, since output streaming happens in real time regardless of the container's eventual removal.
docker run --rm myapp:1.0 generate-report.sh > report.txt
Checking the Result of a One-Off Container's Task
A one-off container's exit code remains available (briefly, before automatic removal) to confirm whether its task actually succeeded.
docker run --rm myapp:1.0 run-checks.sh
echo "Exit code: $?"
Why One-Off Containers Matter
Recognizing a container's role as a one-off task, rather than a persistent service, clarifies appropriate configuration choices — particularly the use of --rm — and helps avoid both unnecessary container accumulation and the conceptual confusion of treating a finite task the same way as an ongoing service.