docker: Compose exec assumes interactive TTY

Tags: til docker linux

docker compose exec ... is different to docker exec ..., and this can cause surprising issues when running these commands in a pipeline. By default compose:

So in a script it can be useful to sniff stdin and disable TTY if it's not interactive:

local execFlags=()

# Do not allocated a pseudo-TTY if *stdin* (file 0) is not a terminal i.e. if piping stdin.
# See: `help test`
if [ ! -t 0 ]; then
  execFlags+=(--no-tty)
fi

exec docker compose exec "${execFlags[@]}" $@"

See:

Published on: 27 Oct 2025