# docker compose exec Command Reference

> Reference for docker compose exec in CI: run a command in a running service container with -T, -e, and -w to drive migrations or checks in pipelines.

Source: https://latchkey.dev/learn/command-reference/docker-compose-exec-command-reference  
Updated: 2026-06-26

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.

## FAQ

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

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
