Run a command in an already-running service container.
docker compose exec runs a command inside a service container that compose up already started, unlike compose run which creates a fresh container. In CI it executes migrations, health probes, or admin commands against the live stack.
Common flags
-T - disable pseudo-TTY allocation (required in CI)
-e KEY=value - set an environment variable for the command
-w, --workdir - working directory inside the container
Always pass -T in pipelines or compose exec fails with "the input device is not a TTY". Use exec (not run) against services already started by compose up so migrations operate on the same database the tests will hit.
Key takeaways
-T disables the TTY so compose exec works in CI.
exec targets a running service; run creates a new container.
Use it for migrations and probes against the live stack.
Frequently asked questions
docker compose exec Command Reference?
docker compose exec runs a command inside a service container that compose up already started, unlike compose run which creates a fresh container. In CI it executes migrations, health probes, or admin commands against the live stack.
In CI?
Always pass -T in pipelines or compose exec fails with "the input device is not a TTY". Use exec (not run) against services already started by compose up so migrations operate on the same database the tests will hit.