48

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

artburkart
  • 1,830
  • 1
  • 20
  • 28
  • II can't figure out what would be the expected outcome of `-i -d` – Auzias Feb 17 '16 at 19:16
  • 4
    @Auzias `-i -d` is in the example I link below with https://github.com/docker/docker/blob/e4cfd9b3924fae0369956b4f0e7f73a7e3b0cbf7/integration-cli/docker_cli_attach_test.go#L139: you launch and detach a process, to which you can attach to and use stdin to feed said process with data. – VonC Feb 17 '16 at 19:20

1 Answers1

49

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

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

(Source: issue 14221)

Or:

$ echo hello | docker run --rm -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

As noted in the comments by Pixelbog, adding the --rm option avoid leaving a bunch of 'Exited' containers.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Could you please step-by-step explain what is exactly going on in the first snippet? Does it end up creating 3 different containers? Does `printf "line1\nline2\n"` run inside the first container? Does the first container write `"line1\nline2\n"` to host console? Do the containers exit after printing "hello"? – Andrzej Gis Mar 11 '17 at 22:44
  • 3
    @gisek 3 containers are run in succession, each tome terminating right after their `echo`/`sed` operation.. The `-i` allows for the stdin to get the stdout produced by the previous container. – VonC Mar 11 '17 at 22:47
  • Thanks. After it all finishes, do we end up with 3 newly created stopped containers? – Andrzej Gis Mar 11 '17 at 22:55
  • 4
    @gisek Yes. A `docker container prune` will take care of them (docker 1.13+/docker 17.03 ce: http://stackoverflow.com/a/32723127/6309) – VonC Mar 11 '17 at 22:58
  • 1
    @AndrzejGis couple of years too late, but you can also do: `docker run --rm ...`, so docker will delete these newly created, now stopped containers :) – Pixelbog Jan 19 '23 at 14:11
  • 1
    @Pixelbog Good point. I have included the `--rm` option in the answer. – VonC Jan 19 '23 at 20:04