Skip to content
Latchkey

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:migrate

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.

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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →