✦ For everyone, free.

Practical knowledge for real and everyday life

Home

19.2.1.3 Run Interactive Option

A focused guide to Run Interactive Option, connecting core concepts with practical Docker and container operations.

The interactive option in docker run enables two-way communication between the terminal and the container's process. It is controlled by two related flags: -i (or --interactive) and -t (or --tty). These flags are almost always used together as -it, though they are technically independent options that can be used separately.

The Two Flags Explained

-i (Interactive)

The -i flag keeps the container's standard input (stdin) open, even if no terminal is attached. Without this flag, stdin is closed immediately when the container starts, making it impossible to type commands into the container or pipe data to it.

-t (Allocate a Pseudo-TTY)

The -t flag allocates a pseudo-terminal (PTY) that is attached to the container's process. This makes the container's output behave as a proper terminal: line buffering works correctly, ANSI escape codes are interpreted (enabling colored output, cursor movement, and command-line editing), and interactive programs like bash, python, vim, and interactive package managers behave as expected.

Without -t, the container process does not have a TTY. Programs that require a terminal (for prompts, menus, or line editing) either fail or behave incorrectly.

Combined Usage

The canonical form opens an interactive shell in a container:

docker run -it ubuntu:22.04 bash

This creates a new Ubuntu container, starts a bash shell inside it, and connects the terminal to the shell. Everything typed in the terminal is sent to bash inside the container, and all output from the shell is printed in the current terminal. The experience is identical to logging into a remote server.

Exiting the shell (by typing exit or pressing Ctrl+D) stops the container because bash was the container's primary process (PID 1).

Using -i Without -t

-i without -t is used when piping data into the container rather than interacting with a TTY:

echo "Hello from outside" | docker run -i ubuntu:22.04 cat

Output:

Hello from outside

The container runs cat, which reads from stdin (which receives the piped data) and prints it to stdout. No terminal is needed because this is a pure pipe, not an interactive session. Adding -t here would be incorrect and could corrupt the pipe output with TTY control sequences.

Using -t Without -i

-t without -i allocates a TTY but immediately closes stdin. The container starts with a terminal allocated but no input possible. This is uncommon and usually unintended.

Practical Interactive Use Cases

Exploring a Container's Filesystem
docker run -it --rm ubuntu:22.04 bash

The --rm flag ensures the container is deleted after the shell session ends, leaving no stopped container behind.

Debugging an Application's Environment
docker run -it --rm myapp:1.0.0 sh

This starts the container but runs sh instead of the application. Useful for inspecting files, environment variables, and dependencies.

Running an Interactive Python REPL
docker run -it --rm python:3.12 python3

This opens the Python interactive interpreter inside a container. All packages installed in the image are available.

Installing Software Interactively
docker run -it ubuntu:22.04 bash
# Inside the container:
apt-get update && apt-get install -y curl

Package managers use TTY detection to decide whether to show progress bars and confirmation prompts. Without -t, they may fall back to minimal output or fail on interactive prompts.

Interactive Mode with Existing Containers

For a container already running in the background, docker exec is used instead of docker run:

docker exec -it my-running-container bash

This opens an additional shell process in the running container without affecting the container's primary process.

What Happens When You Exit

When you exit the shell in an interactive container started with docker run -it, the container's primary process ends. The container transitions from Running to Stopped (Exited). It remains in docker ps -a until removed.

To prevent this and keep the container for later reuse:

docker run -it --name dev-box ubuntu:22.04 bash
# ... work inside the container ...
# Press Ctrl+P then Ctrl+Q to detach without stopping

After detaching with Ctrl+P Ctrl+Q, the container keeps running. Reattach later with:

docker attach dev-box

Interactive Mode in Automation

Interactive flags should not be used in scripts or CI pipelines. Programs that detect no TTY often behave differently:

# This may hang or produce unexpected output in a non-interactive environment
docker run -it myapp:1.0.0 ./install.sh

In automated contexts, use:

docker run myapp:1.0.0 ./install.sh

Or pass -i if stdin piping is needed:

echo "yes" | docker run -i myapp:1.0.0 ./interactive-installer.sh

Summary of Flag Behaviors

FlagsstdinTTYBest for
(none)closednoNon-interactive commands, output to log
-iopennoPiping data into the container
-tclosedyesRarely used; provides TTY but no input
-itopenyesInteractive shells, REPLs, debugging