Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would I use `--interactive` without `--tty` in a Docker container?

Tags:

docker

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

like image 878
artburkart Avatar asked Feb 17 '16 14:02

artburkart


People also ask

What does docker run -- interactive do?

Docker allows you to run a container in interactive mode. This means you can execute commands inside the container while it is still running. The command prompt will change, moving you to the bash shell as in the example below.

What is TTY in docker container?

The -t (or --tty) flag tells Docker to allocate a virtual terminal session within the container. This is commonly used with the -i (or --interactive) option, which keeps STDIN open even if running in detached mode (more about that later).

What is interactive mode and detached mode in docker?

-i (interactive) is about whether to keep stdin open (some programs, like bash, use stdin and other programs don't). -d (detached) is about whether the docker run command waits for the process being run to exit. Thus, they are orthogonal and not inherently contradictory.

What is the command to enter interactive console on docker?

If you need to start an interactive shell inside a Docker Container, perhaps to explore the filesystem or debug running processes, use docker exec with the -i and -t flags. The -i flag keeps input open to the container, and the -t flag creates a pseudo-terminal that the shell can attach to.


1 Answers

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

like image 158
VonC Avatar answered Oct 11 '22 10:10

VonC