Difficulty: Beginner
What do the -i and -t flags do in docker run -it, and what happens if you use only one of them?
-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.
-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.| Flags | Input | Terminal | Typical use | |
|---|---|---|---|---|
| none | Closed | No | Servers and batch jobs, usually with -d | |
-i only | Open | No | Piping data in: `cat dump.sql \ | docker run -i postgres psql ...` |
-t only | Closed | Yes | Rarely useful; you see a prompt but cannot type | |
-it | Open | Yes | Interactive 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.
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.
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.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.docker exec -it api sh follows the same rules as docker run.| -i (interactive) | -t (tty) | |
|---|---|---|
| What it does | Keeps STDIN open | Allocates a pseudo-terminal |
| Without it | Process reads EOF at once | No prompt, no colours, plain output |
| Alone is useful for | Piping files into a container | Almost nothing |
| Safe in CI pipelines | Yes | No, fails with not a TTY |
# 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'.
-i interactive, -t pseudo-TTY, STDIN, Detach keys, Not a TTY error