docker run -i -t --name nodejs ubuntu:latest /bin/bash
So here, -i
stands for interactive mode and -t
will allocate a pseudo terminal for us.
Some more trivia about these flags.
-i
keeps STDIN
open even if not attached and it allows piping (or “composition”).
Since -i keeps STDIN
open even if not attached, it allows for composition (piping).
So for example
docker run ubuntu printf "line1\nline2" \
| docker run -i ubuntu grep line2 \
| docker run -i ubuntu sed 's/line2/line3/g'
Or, for a simpler example,
echo hello | docker run -i busybox cat
hello
The -t
option goes back to how Unix/Linux handles terminal access:
In the past, a terminal was a hardline connection. Terminals had physical device drivers which were real pieces of equipment.
Once generalized networks came into use, a pseudo-terminal driver was developed.
The pseudo-terminal driver is a layer of abstraction that helps the end user interact with the terminal.
So, with that background, when you do a docker run container
(without any -i
or -t
flag), you basically get a STDOUT
stream. So this works:
docker run ubuntu echo "hello" | cat
With -i
you add a STDIN
to docker too, so…
echo "hello" | docker run -i busybox cat
…works as well.
And with -t
you add the pseudo-tty driver to the container, so you can type stuff from your keyboard into the container process; this is what you want (*typically combined with -i
as in -it
**) when you want to interact with the container.
The -i -t
makes the container start to look like a terminal connection session.
If you are interested in how Docker actually does what it does, there is no better way than reading the source to learn more about it.
I didn't get what the meaning of docker run -it ?