You pipe something into docker compose exec from a script or cron job and get a strange error, or garbled output. Compose is asking for a terminal that is not there.
By default, exec allocates a pseudo-TTY, which is right for an interactive shell and wrong for automation. The -T flag turns it off.
docker compose exec -T db pg_dump -U postgres mydb > backup.sql
Without -T, the TTY can mess with line endings in the dump and error messages about the input not being a terminal show up in cron mail. With it, the output is clean bytes and the redirect does what you expect.
The same idea applies to docker compose run and plain docker exec, where you drop the -t flag and keep -i only when you feed it input.
The caveat: with no TTY there are no colours and no prompts that need a keyboard. If the command asks a question, it will hang or fail, so pass whatever flag makes it non-interactive. And you need the service to be running, since exec works on a live container only.
Further reading: docker compose exec, On this site: docker compose watch syncs your code into a running container, On this site: ${VAR:?message} makes Compose stop when a variable is missing, On this site: Guide: install Pi-hole on Ubuntu, with the script or with Docker Compose