Skip to content

Instantly share code, notes, and snippets.

@nhannguyen95
Forked from v0lkan/docker-i-t-trivia.md
Created January 22, 2019 06:01
Show Gist options
  • Save nhannguyen95/4ca36bc6ec43e7543dc4589cd437a844 to your computer and use it in GitHub Desktop.
Save nhannguyen95/4ca36bc6ec43e7543dc4589cd437a844 to your computer and use it in GitHub Desktop.
docker run -i -t
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment