I read some docker docs and I do not understand what it might mean to
- attach a tty
- attach std-in and std-out
for these purposes, I see that -i
and -t
flags are use.
What does this process mean?
I read some docker docs and I do not understand what it might mean to
for these purposes, I see that -i
and -t
flags are use.
What does this process mean?
We can see what is happening under the hood by using the
lsof
command. For a demonstration we can create a simple docker container from a Debian image that just runs sleep:This will start the sleep command in a new container (note that we did not use
-i
or-t
).Next we "login" into our container though the
exec
command and start a bash:A plain debian image will not have
lsof
installed so we need to install it:Next we run lsof:
If run without any options,
lsof
will print open files for all running processes. You should see three processes in its output (sleep, bash, and lsof itself).Here are the relevant lines are those that show the file descriptors (FD column)
0
to2
.Note how the
sleep
process, which we started without the -t option has three FIFO pipes forstdin
,stdout
andstderr
:While the
bash
process has an actual device attached tostdin
,stdout
andstderr
:Lets create another container with the
-t
option:After installing
lsof
again (see above) we get a different output fromlsof
for thesleep
process:Note how the type column has changed to
CHR
and the name column shows/15
.Finally, when we omit the
-t
option from theexec
command and like this:then we can notice two things. First, we do not get a shell prompt from the bash now, but we can still type commands and see their output. When we run
lsof
we see that thebash
process now also has pipes rather then a tty attached tostdin
,stdout
, andstderr
It means you can log in to your container using TTY, ie terminal. It's as if you've got a Linux machine in front of you and you're logging into it. If you have a container that's not running SSH server or telnet, this is your only mode of getting into the command line prompt.
As for why
-i
and-t
are different arguments I'm not sure about, I can't imagine a scenario where you want to connect using TTY and don't want the stdin/stdout option or vice versa.Simply put it allows us to attach and detach from the containers terminal. To attach we use the docker attach command and to detach we use the CTRL+P & CTRL+Q command.
stdin, stdout, and ttys
are related concepts.stdin
andstdout
are the input and output streams of a process. A pseudo terminal (also known as atty
or apts
) connects a user's "terminal" with thestdin
andstdout
stream, commonly (but not necessarily) through a shell such asbash
. I use quotes around "terminal" since we really don't use a terminal in the same sense today.In the case of docker, you'll often use
-t
and-i
together when you run processes in interactive mode, such as when starting abash
shell. In the case of the shell you want to be able to issue commands and read the output.Some examples of experimenting with
-t
and-i
(Docker version 0.8.1):The code docker uses to attach
stdout/stdin
has all the dirty details.