12

The -i flag is described as "Keep STDIN open even if not attached", but Docker run reference also says:

If you do not specify -a then Docker will attach all standard streams.

So, by default, stdin is attached, but not opened? I think it doesn't make any sense when STDIN is attached but not opened, right?

Mohsin Aljiwala
  • 2,457
  • 2
  • 23
  • 30
YON
  • 1,607
  • 3
  • 18
  • 33

1 Answers1

9

The exact code associated with that documentation is:

// If neither -d or -a are set, attach to everything by default
if len(flAttach) == 0 && !*flDetach {
    if !*flDetach {
        flAttach.Set("stdout")
        flAttach.Set("stderr")
        if *flStdin {
            flAttach.Set("stdin")
        }
    }
}

With:

flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached")

In other words, stdin is attached only if -i is set.

        if *flStdin {
            flAttach.Set("stdin")
        }

In that sense, "all" standard streams isn't accurate.

As commented below, that code (referenced by the doc) has since changed to:

cmd.Var(&flAttach, []string{"a", "-attach"}, "Attach to STDIN, STDOUT or STDERR")

-a does not man anymore "attach all streams", but "specify which streams you want attached".

var (
    attachStdin  = flAttach.Get("stdin")
    attachStdout = flAttach.Get("stdout")
    attachStderr = flAttach.Get("stderr")
)

-i remains a valid option:

if *flStdin {
    attachStdin = true
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The source you provided is out of date. The latest link is [this](https://github.com/docker/docker/blob/07f580489908bf6a3373daac1473045406e1130d/runconfig/opts/parse.go#L138). However, it seems that I can force set attach STDIN, for example `docker run -a stdin python:2` which will set attachStdin true. – YON Apr 12 '16 at 07:03
  • @Yon True, I have edited the answer accordingly, illustrating the new meaning of `-a`. – VonC Apr 12 '16 at 07:09
  • Thanks. But, I still don't know is there any chance that make `attachStdin` true while `flStdin` false is useful? If not, why don't just set `attachStdin = flStdin` – YON Apr 12 '16 at 07:46
  • @Yon It seems it is just another way to specify you want stdin attached: `-a stdin` and `-i` are equivalent. – VonC Apr 12 '16 at 07:47
  • 2
    No, I don't think so. `docker run -a stdin -a stdout -a stderr -t python:2` and `docker run -it python:2` is not same, I can not input anything in the former. – YON Apr 12 '16 at 07:54
  • @Yon You should, does piping work? (http://stackoverflow.com/a/35462735/6309) Can you try with -t? (http://stackoverflow.com/a/35551071/6309) – VonC Apr 12 '16 at 07:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108891/discussion-between-yon-and-vonc). – YON Apr 12 '16 at 08:07
  • Never mind. Here is what I said in chat, "`echo hello | docker run -i busybox cat` prints `hello` as expected, while `echo hello | docker run -a stdin -a stdout busybox cat` prints nothing..." – YON Apr 12 '16 at 12:05
  • @Yon Strange, and `echo hello | docker run -t -a stdin -a stdout busybox cat`? – VonC Apr 12 '16 at 12:08
  • `cannot enable tty mode on non tty input`, pipe is conflict with `-t` – YON Apr 12 '16 at 12:11
  • @Yon makes sense. That seems confusing, as the code seems to make `-i` and `-a stdin` identical (in that both attach stdin) – VonC Apr 12 '16 at 12:12
  • 2
    From code, `-i` is passed to container's config ([link](https://github.com/docker/docker/blob/07f580489908bf6a3373daac1473045406e1130d/runconfig/opts/parse.go#L380)), so in my opinion, `-i` is about **open STDIN in container**, `-a` is about **attach host's STDIN to container's STDIN** (same for STDOUT and STDERR). I just don't know why docker allow attaching STDIN to container's STDIN even if it is not open. – YON Apr 12 '16 at 12:20
  • Maybe I need to dive into code more, however I don't know Go. (⊙_⊙) – YON Apr 12 '16 at 12:26
  • @Yon -i is used for piping from host to container though. – VonC Apr 12 '16 at 12:43
  • I think it is because `-i` will force attach STDIN and open container's STDIN so that we can use pipe. – YON Apr 12 '16 at 12:47