brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: use docker compose exec -T when a script runs it

Docker
~/tips/tip-docker-exec-no-tty

docker compose exec -T db pg_dump -U postgres mydb > backup.sql

Runs a command in a service without a terminal, so output can be piped or redirected cleanly.

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

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link