Interactive Terminal

Difficulty: Beginner

Question

What do the -i and -t flags do in docker run -it, and what happens if you use only one of them?

Answer

-i (--interactive) keeps the container's STDIN open so you can send input to it, and -t (--tty) allocates a pseudo-terminal so the process behaves as if it is talking to a real terminal, with a prompt, line editing and colours. You combine them as -it when you want an interactive shell, such as docker run -it ubuntu bash.

What each flag actually changes

  • -i wires up input. Without it, STDIN is closed and the process sees end-of-file immediately. With it, whatever you type or pipe in reaches the process.
  • -t changes how the process sees its output device. Programs check isatty(). With a TTY, bash prints a prompt, ls uses colours and columns, Python opens its REPL, and Ctrl-C is delivered as a signal through the terminal. Without a TTY, many programs switch to plain, non-interactive behaviour.

The four combinations

FlagsInputTerminalTypical use
noneClosedNoServers and batch jobs, usually with -d
-i onlyOpenNoPiping data in: `cat dump.sql \docker run -i postgres psql ...`
-t onlyClosedYesRarely useful; you see a prompt but cannot type
-itOpenYesInteractive shells, REPLs, debugging

The -i-only row is the one people forget. Scripts and CI pipelines that pipe input should use -i without -t, because a TTY also merges stderr into stdout and turns line endings into \r\n, which corrupts binary or machine-parsed output.

The "input device is not a TTY" error

In a GitHub Actions job, a Jenkins pipeline or a cron script there is no terminal attached. Running docker run -it ... or docker exec -it ... there fails with "the input device is not a TTY". The fix is to drop -t (keep -i if you pipe input). This is one of the most common CI failures when a developer copies a command from their laptop into a pipeline.

Detaching and other gotchas

  • Detach without stopping: press Ctrl-P then Ctrl-Q to leave a container started with -it running in the background. exit or Ctrl-D ends the shell, and if the shell is PID 1 the container stops.
  • Reattach: docker attach <name> reconnects to PID 1's streams; docker exec -it <name> sh starts a new shell instead, which is usually what you want for debugging.
  • -dit starts a container in the background with a TTY and open STDIN, a common trick to keep a bare ubuntu container alive for experiments.
  • Same flags for exec: docker exec -it api sh follows the same rules as docker run.
-i versus -t. The two flags solve different problems and are combined for interactive shells.
-i (interactive)-t (tty)
What it doesKeeps STDIN openAllocates a pseudo-terminal
Without itProcess reads EOF at onceNo prompt, no colours, plain output
Alone is useful forPiping files into a containerAlmost nothing
Safe in CI pipelinesYesNo, fails with not a TTY

Code examples

Interactive shell versus piped input

# Interactive shell: needs both flags
docker run -it --rm python:3.12-slim python
# >>> prompt appears, arrow keys and Ctrl-C work

# Piping data in (CI-safe): -i only, no -t
echo "SELECT count(*) FROM orders;" | \
  docker exec -i pg psql -U app -d shop -t

# Debug a running container with a new shell
docker exec -it api sh

# Leave an -it container running: press Ctrl-P then Ctrl-Q

The psql example works in a pipeline because it does not ask for a TTY. Adding -t there would fail in CI with 'the input device is not a TTY'.

Key points

  • -i keeps STDIN open; -t allocates a pseudo-TTY
  • Use -it for shells and REPLs, -i alone for piping input, neither for servers
  • In CI, -t fails with 'the input device is not a TTY'
  • A TTY merges stderr into stdout and changes line endings, so avoid it for machine-parsed output
  • Ctrl-P Ctrl-Q detaches without stopping the container

Common follow-up questions

  • What is the difference between docker attach and docker exec?
  • Why does a container started with docker run ubuntu exit immediately?
  • How do signals like Ctrl-C reach the process inside a container?

Interview tips

  • Explain each flag separately before the combination; it proves you know why, not just what
  • Bring up the CI 'not a TTY' error; it shows real hands-on experience

Concepts covered

-i interactive, -t pseudo-TTY, STDIN, Detach keys, Not a TTY error

More Docker & Containers questions