When would I use `--interactive` without `--tty` i

2019-01-22 12:43发布

问题:

I did some googling and have had no luck finding a case where I'd run docker run -i some_image rather than docker run -it some_image.

If I run docker run -i --name sample some_image bash, the container runs in the foreground, but I can't interact with it from the shell I'm in. I can't even stop it with CTRL+C. I can, however, pop open another shell and run docker exec -it sample bash and gain access to the container.

If I run docker run -i -d --name sample some_image bash, the container immediately exits. I can restart it with docker start sample and then it stays up, so I can run docker exec -it sample bash and interact with it again.

However, in all these cases, I ultimately end up using -it to interact with my containers. In what world would I not need the -t flag?

Cheers

回答1:

Since -i keeps STDIN open even if not attached, it allows for composition (piping).
For example:

docker run ubuntu printf "line1\nline2\n" | docker run -i ubuntu grep line2 | docker run -i ubuntu sed 's/line2/line3/g'

(Source: issue 14221)

Or:

$ echo hello | docker run -i busybox cat
  hello

(Source: issue 12401)

Now imagine this not in front of a keyboard and being used in a script where you can actually write to the processes stdin through something better than a shell |: example integration-cli/docker_cli_attach_test.go



标签: docker