Skip to content

Instantly share code, notes, and snippets.

@v0lkan
Last active July 25, 2023 04:35
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save v0lkan/c413cf9477b607db1ea1117c9de853df to your computer and use it in GitHub Desktop.
Save v0lkan/c413cf9477b607db1ea1117c9de853df 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.

@mahmmoudkinawy
Copy link

I didn't get what the meaning of docker run -it ?

@v0lkan
Copy link
Author

v0lkan commented Nov 16, 2021

^ The article precisely explains the meaning of docker run -it :)

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