Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 93/ 160
05. Docker Compose & Multi-Container Apps

Running One-Off Tasks & Migrations (docker compose run, exec)

Run migrations and shells with compose run/exec without reshaping long-lived services.

Beginnercompose run · exec3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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 /app for 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

Loading interactive 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.

Concrete compose run setup

docker compose run --rm migrate alembic upgrade head; docker compose exec -T api python -m compileall /app

Key takeaway: Prove exec with 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.

Deliberate failure test

Exercise this failure safely: using exec for a migration when the API is down, or forgetting --rm and accumulating stopped one-off containers.

Key takeaway: compose run preserves service config with a bounded lifecycle; a dedicated CI job is cleaner when credentials or environment differ substantially.

How it works, step by step

Step 1 of 4
Design

1. 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.

Give database migrations a one-off lifecycle and an exit code while keeping the long-running API container immutable.
Key numbers at this step
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 remains in 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

Explicit compose run
Best for: Run migrations and shells with compose run/exec without reshaping long-lived services, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived exec experiments with low blast radius.

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.

Managed Migrations
Best for: Teams that centralize Migrations policy across many workloads.

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 /app as proof and skipping capture 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 /app address 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Extending Services with extends, YAML Anchors & include
Next topic
Live Development Workflows with docker compose watch (Sync & Rebuild)