Working with Stdin, TTY, and Interactive Shells in Docker
Master interactive Docker containers using -i (stdin) and -t (TTY) flags, pseudo-terminal allocation mechanics, entering running containers, and piping streams into docker run/exec.
Published on • November 17, 2026
AI Assistant

Running interactive terminal shells or streaming input into Docker containers is a fundamental daily task for developers and sysadmins. When running commands like docker run -it alpine sh or docker exec -it web bash, flags like -i and -t play distinct roles.
Understanding standard input (stdin), pseudo-terminal (TTY) allocation, and stream piping mechanics allows you to troubleshoot issues, interact with containerized applications, and automate shell pipelines.
1. Deconstructing -i (Interactive / Stdin) and -t (TTY)
While -i and -t are frequently combined as -it, they control two entirely separate operating system mechanics:
| Flag | Name | Functionality |
|---|---|---|
-i | --interactive | Keeps standard input (stdin) open even if not attached, enabling data piping into the container. |
-t | --tty | Allocates a pseudo-terminal (pty) inside the container, providing terminal formatting, colored text, and prompt handling. |
2. When to Use -i, -t, or -it
Combination A: Interactive Terminal (-it)
When you want an interactive REPL shell (e.g., bash, sh, python, node) where you type commands and expect formatted prompts:
docker run --rm -it ubuntu:latest bash
Here:
-iconnects your keyboard input to the shell’sstdin.-tprovides terminal capabilities (cursor movement, color ANSI codes,Ctrl+Cinterrupt handling, line editing).
Combination B: Piping Data into Containers (-i without -t)
When piping file streams or script contents into a container process via stdin, do not pass -t:
# Pipe host SQL script directly into a containerized PostgreSQL database
cat schema.sql | docker run -i --rm postgres:alpine psql -U postgres -d mydb
Passing -t during piped operations forces pseudo-terminal allocation, causing line-ending translation errors (CRLF vs LF) or binary stream corruption.
Combination C: Headless Script Execution (Neither -i nor -t)
For background automated scripts where output is captured without user interaction:
docker run --rm alpine echo "Build completed successfully"
3. Entering Running Containers with docker exec -it
To open an interactive diagnostic shell inside an already running container instance, use docker exec -it:
# Execute bash inside a running NGINX container
docker exec -it web-server bash
If the target container does not include bash (e.g., minimal Alpine Linux images), fall back to POSIX sh:
docker exec -it web-server sh
4. Detaching from Interactive Sessions Without Stopping
When running an interactive container session (docker run -it ...), pressing Ctrl+C or typing exit sends a signal to PID 1, which stops the container.
To detach from a running interactive session without stopping the container, use the default escape key sequence:
Ctrl + P, followed by Ctrl + Q
The container detaches and continues running in the background. You can re-attach to it later using docker attach:
docker attach web-server
5. Summary and Best Practices
Takeaway: Use -it together when opening interactive terminal shells like bash or sh. Use -i alone when piping raw text or binary data streams into containerized commands, omit both for automated non-interactive tasks, and use Ctrl+P + Ctrl+Q to detach from interactive sessions cleanly without terminating the container.