Search code examples
bashdockerttypty

Get `the input device is not a TTY` error when run docker command inside a while loop


I have below bash script. It run docker run command inside a while loop which read from a text file line by line.

#!/bin/sh -eu

while IFS= read -r url; do
    docker run --rm -it alpine ls
done < repo_list.txt

I get this error the input device is not a TTY when executing the above script. However, it works fine if I remove the while loop like this:

#!/bin/sh -eu
docker run --rm -it alpine ls

I know that I can fix it by removing the -it parameter from the docker command. But I'd like to know why the while loop cause this error. Is there any way I can fix it without changing the docker command?


Solution

  • In your second example, you forgot to change stdin like you've done in your while loop:

    < repo_list.txt
    

    With input as a file, you cannot enable interactive and tty support simultaneously, because the file is not a tty device.