- Running the script directly on the host behaves as expected:
- If you dockerize that application and run the image without special flags, the container runs non-interactively: it isn’t attached to your keyboard or terminal device, so it prints only what goes to standard output and then exits.
- By default
docker rundoes not attach the container to your standard input (stdin) and does not allocate a pseudo-TTY. Even when you invokedocker runfrom a terminal, the container process is not connected to your keyboard or terminal device — it runs in non-interactive mode.
-i (interactive)
- To keep the container’s standard input open so it can read data from you, add
-i:
- In this example the container read the input, but the prompt text (the “Welcome! Please enter your name:” line) may not be visible because many programs print prompts only when they detect they are attached to a terminal (a TTY).
-t
- To make the program inside the container think it’s talking to a real terminal so it will print prompts, colorized output, progress bars, etc., allocate a pseudo-TTY with
-t. Combine-iand-tfor a full interactive session:
Common use cases and patterns
- Use
-ialone when you want to pipe data into a container (stdin connected) but you don’t need a terminal. Example: streaming an SQL dump into a database container. - Use
-talone when you want the program to detect a terminal (for colored output or interactive formatting) but you don’t need to type into it. - For interactive command-line programs (REPLs, shells, text-based installers) you typically use
-ittogether.
Use
-i when piping/streaming data into a container. Use -t when you want the program to detect a terminal (colors, prompts). Use -it for an interactive terminal session.- Docker run reference: https://docs.docker.com/engine/reference/commandline/run/
- Understanding TTY and stdin in containers: https://docs.docker.com/engine/reference/commandline/attach/