docker compose exec Command Reference
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-u, --user- user to run as--index- target a specific replica when scaled
Example
shell
docker compose up -d --wait
docker compose exec -T db pg_isready -U postgres
docker compose exec -T -e RAILS_ENV=test app bin/rails db:migrateIn 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.
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.
Related guides
docker compose run Command ReferenceReference for docker compose run in CI: run a one-off command in a service container with --rm, -e, and --no-…
docker compose up Command ReferenceReference for docker compose up in CI: start services from a compose file with -d, --build, and --wait so dep…
docker exec Command ReferenceReference for docker exec in CI: run a command inside a running container with -e, -w, -u, and -i to drive mi…