Search code examples
linuxdockershtty

How to redirtect terminal STDIN when running a script in Docker?


I have a script that consumes users input. It is run like this:

./script <<EOF
> input
> onther input
> more input
> EOF

I need to distribute the script as a Docker image.

I am able to run the script in two steps. First, I get into the containers shell.

docker run -it my-docker-tag sh

And then, inside the shell, I execute the script itself.

Question: Is it possible to run the script in on shut (without having to navigate to the containers shall)?

I tried this:

docker run -it my-docker-tag ./script <<EOF
> input
> onther input
> more input
> EOF

But it fails with:

the input device is not a TTY


Solution

  • The Docker run reference notes:

    Specifying -t is forbidden when the client is receiving its standard input from a pipe, as in:

    $ echo test | docker run -i busybox cat
    

    If you remove the -t option, shell pipes and redirections around a (foreground) docker run command will work as you expect.

    # A heredoc as in the question should work too
    sudo docker run --rm my-docker-tag ./script <script-input.txt