Trying to understand the relation of tty between the host and container when the docker run command is executed

About Docker if is executed the following command

docker run --name ubuntu-it -it ubuntu

The container named ubuntu-it, based on the ubuntu image, is created, run, is offered a tty and finally remains running. It can be stopped with the exit command. Here the importance about the it option to have a shell available for human interaction. It to have available the features about the STDIN and STDOUT/STDERR streams within the current running container through the shell within the tty.

For experimental purposes, if is executed the following commands

docker run --name ubuntu-a ubuntu ls
docker run --name ubuntu-b ubuntu date

The containers named ubuntu-a and ubuntu-b, based on the ubuntu image, are created, run, are listed/displayed the execution of the ls and date commands and finally the containers are stopped.

The ls and date commands are executed within the running containers prior to be stopped.

Question

If the it option was not used for each docker run command.

  • Why in the host’s tty is possible see the listed/displayed data of the ls and date commands?

  • Both of these commands just print to stdout. They don’t read from their stdin and they don’t care whether their stdout is a tty or not.

    – 

  • 1

    this post goes into a fair amount of detail on the tty mechanism within docker

    – 

  • @DavidMaze Both of these commands just print to stdout – yes, but it is internally within the container. So: Why the host’s tty is able to see the data? – I thought the a option should be mandatory. It is something similar than the following situation – stackoverflow.com/a/78054091/3665178

    – 




  • docker run (without a -d option) prints the process’s stdout to its own stdout. There’s no particular reason you need a tty for most programs. (Consider why you see output from plain ls, or a more involved ls </dev/null 2>&1 | tee /dev/null that’s more forcibly disconnected from the terminal.)

    – 

Leave a Comment