Running One-Off Tasks & Migrations (docker compose run, exec)
Run migrations and shells with compose run/exec without reshaping long-lived services.
In plain words
Give database migrations a one-off lifecycle and an exit code while keeping the long-running API container immutable.
Think of it like…
run hires a short-lived contractor for one job; exec knocks on the existing worker’s door and hands them another task.
Where you’ve seen it
In practice, docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app is the concrete move for this job: run migrations and shells with compose run/exec without reshaping long-lived services. The release is not accepted until capture the migrate exit code, inspect the schema version and confirm no named migration container remains confirms the result.
Words you’ll hear
- compose run
- The implementation control in
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /appfor run migrations and shells with compose run/exec without reshaping long-lived services. - exec
- The result to observe with
capture the migrate exit code, inspect the schema version and confirm no named migration container remains, rather than infer from a successful command alone. - Migrations
- The production boundary exposed when using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.
Visual diagram
Theory & deep dive
compose run control path
The concrete mechanism is docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app. The observable result is capture the migrate exit code, inspect the schema version and confirm no named migration container remains; if those disagree, inspect the runtime evidence before changing unrelated settings.
Run migrations and shells with compose run/exec without reshaping long-lived services. Keep compose run, exec and Migrations in the same deployment record so the intent and the observed state cannot drift apart.
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app
capture the migrate exit code, inspect the schema version and confirm no named migration container remains, not with command success alone.Migrations failure boundary
A migration launched with run may start dependency containers and use a different network, but it intentionally does not rewrite the service definition. exec has no clean replacement source and can leave debugging file changes in a live container. The narrow failure to design for is using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.
Accept this compromise explicitly: compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially. The controlled check-capture the migrate exit code, inspect the schema version and confirm no named migration container remains-also identifies the point where a different design is safer.
Exercise this failure safely: using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A migration launched with run may start dependency containers and use a different network, but it intentionally does not rewrite the service definition. exec has no clean replacement source and can leave debugging file changes in a live container.
- Intent
- compose run
When to use it & trade-offs
What it is
compose run creates a temporary container for a service and normally opens no published ports unless --service-ports is requested; compose exec starts a process in an already running container. Run is appropriate for migrations/tests, exec for immediate diagnosis.
Why it matters: A migration launched with run may start dependency containers and use a different network, but it intentionally does not rewrite the service definition. exec has no clean replacement source and can leave debugging file changes in a live container.
When to reach for it
- The fit is strongest for this job: run migrations and shells with compose run/exec without reshaping long-lived services. Pair it with
capture the migrate exit code, inspect the schema version and confirm no named migration container remainsin the runbook. - Production compose run changes should make exec observable through
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app, rather than trusting the runtime default. - Keep a rollback example for the case where using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.
When to avoid or be careful
- Do not accept command success while the system can still fail by using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.
- Avoid erasing Migrations protection merely to dodge this cost: “compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially”.
- Do not replace the evidence path-
capture the migrate exit code, inspect the schema version and confirm no named migration container remains-with a screenshot or a mutable tag.
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app: practical choices
Strengths: compose run creates a temporary container for a service and normally opens no published ports unless --service-ports is requested; compose exec starts a process in an already running container. Run is appropriate for migrations/tests, exec for immediate diagnosis.
Watch out: compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially.
Strengths: It can begin with docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app and fewer surrounding controls.
Watch out: It silently accepts using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers, which makes the result hard to reproduce after promotion.
Strengths: A migration launched with run may start dependency containers and use a different network, but it intentionally does not rewrite the service definition. exec has no clean replacement source and can leave debugging file changes in a live container.
Watch out: The platform adds an ownership and debugging layer; retain capture the migrate exit code, inspect the schema version and confirm no named migration container remains as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app
- capture the migrate exit code, inspect the schema version and confirm no named migration container remains
- Failure signature: using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers
- Trade-off: compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially.
Benchmarks & rules of thumb
- 1 exit status
- one-off migration result CI can gate on
- 0 published ports
- default behavior for compose run
- --rm
- removes the temporary container after completion
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /appas proof and skippingcapture the migrate exit code, inspect the schema version and confirm no named migration container remains. - Using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers
- Optimizing compose run without accepting that compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially.
Likely interviewer follow-ups
- Why does
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /appaddress compose run here rather than through application code? - How would you distinguish healthy exec from the failure “using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers” using
capture the migrate exit code, inspect the schema version and confirm no named migration container remains? - When would this trade-off-“compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially”-justify a different Migrations design?
docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app-and one proof-capture the migrate exit code, inspect the schema version and confirm no named migration container remains. Then name the boundary: using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Running One-Off Tasks & Migrations (docker compose run, exec) with generative AI interview Q&A and hands-on practice.