23

I was just reading THIS article on Docker , Its an article describing how to dockerize a simple application. The following command is executed:

$ docker run -t -i ubuntu:14.04 /bin/bash, 

and then, The following explanation is given:

Here we’ve again specified the docker run command and launched an ubuntu:14.04 image. But we’ve also passed in two flags: -t and -i. The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.

I don't understand the meaning of:

-i flag allows us to make an interactive connection by grabbing the standard in (STDIN)

Thank you.

Tenali_raman
  • 2,000
  • 3
  • 18
  • 28

3 Answers3

30

Docker's -i/--interactive allows you to send commands to the container via standard input ("STDIN"), which means you can "interactively" type commands to the pseudo-tty/terminal created by the -t switch.

  • 3
    whats the difference between interactively vs non-interactively ? – Tenali_raman Aug 26 '15 at 16:56
  • 2
    non-interactive means that you can't pass in text to the tty you allocated with `-t`. interactive means you can type commands and the tty in the container will receive this text. –  Aug 26 '15 at 17:01
  • 6
    I am still confused. The meaning of `-i`, according to the docs, is "Keep STDIN open even if not attached". That suggests to me that `-i` should be unnecessary when STDIN is attached and only be needed when the `-d` option or an explicit `-a` not including STDIN is used. However, this is obviously wrong since if I run `docker run -t ubuntu cat`, it doesn't echo input; I have to add `-i` to get that; but I don't understand neither why `-i` is necessary here nor why anyone would want the behavior of `-t` without `-i`. Nor do I see if `-i` is useful with `-d` or `-a`, as the docs imply. – gmr Apr 18 '16 at 05:59
9

I explained here that -i, --interactive keeps STDIN open even if not attached, which you need if you want to type any command at all.

That helps for pipes:

$ echo hello | docker run -i busybox cat
  hello

Meaning: -i does not always need -t (tty), with tty being the text-terminal.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • What does "not attached" mean, exactly? – Vladimir Panteleev Aug 27 '20 at 08:06
  • 1
    @VladimirPanteleev Attached to a tty. See also https://stackoverflow.com/a/36565383/6309 and https://stackoverflow.com/a/35462735/6309 – VonC Aug 27 '20 at 08:25
  • OK, though it would help to clarify what exactly is "attached to a TTY". So, I understand that "not attached" used to mean "`-a` is not specified", but the meaning of `-a` changed and now it's less obvious. So, to be more precise, if `-d` is not specified, lack of `-i` and `-a` is the same as `-a stdout -a stderr`, but with `-i`, it's the same as `-a stdin -a stdout -a stderr`. – Vladimir Panteleev Aug 27 '20 at 12:13
  • @VladimirPanteleev Yes, `-i` would asdd the `-a stdin` indeed. `-i` keep the the container’s stdin available, either to a pseudo tty (`-t`), or to any pipe, when run with `-d`, in background mode. – VonC Aug 27 '20 at 12:18
4

From the docs:

For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process. -i -t is often written -it as you’ll see in later examples. Specifying -t is forbidden when the client is receiving its standard input from a pipe, as in:

$ echo test | docker run -i busybox cat

The -t flag is how Unix/Linux handles terminal access. Historically, a terminal was a hardline connection, with real pieces of hardware.

Today however, a pseudo-terminal driver is used.

  • Run a container with no flags and by default you have a stdout (standard output) stream.
  • Run with the -i flag, and you get a stdin (standard input) stream added, which accepts text as input.
  • Run with -t, usually with the -i and you have a terminal driver added, which if you want to interact with the process is likely what you want. It basically makes the container start to look and feel like a terminal session.

Running -t without -i, means that you will have the terminal, but your input will not be connected to the terminal input.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
  • In what cases do we need `-t` without `-i`? If there is no case, why don't Docker just make `-t` get stdin automatically? – Shuai Jul 07 '21 at 01:58