Docker : ' -ti ' argument - docker

Build an image based on the Dockerfile:
docker image build .
The result of above is
docker run --rm -ti <IMAGE_ID>
--rm : If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the --rm flag src : https://docs.docker.com/engine/reference/run/
What does the -ti argument achieve ?

-it is shorthand for -i -t
By Docker Run docs
-i Keep STDIN open even if not attached
-t Allocate a pseudo-tty
Which means, t for open a shell terminal (telewriter) and i listen to standard input.

It's basically just -i and -t which is mostly written as -it.
-i:
Keep STDIN open even if not attached
-t:
Allocate a pseudo-TTY
See: https://docs.docker.com/engine/reference/commandline/exec/

It's most common way of using Docker containers, so you can actually use them to execute some of your commands.
i stands for interactive and it's accepts and responds on your input using STDIN.
t is for you to have terminal - it's combined with i so you can actually put your input there.
Check more here and here.

Related

running docker container without /bin/bash command

I create docker container with
sudo docker run -it ubuntu /bin/bash
in book The docker book I read
The container only runs for as long as the command we specified, /bin/bash , is running.
didn't I created terminal with option -it and /bin/bash isn't required? will anything change if I don't pass any command in docker run?
You will get the same behavior if you run
sudo docker run -it ubuntu
because the ubuntu docker image specifies /bin/bash as the default command. You can see that in the ubuntu Dockerfile. As #tadman wrote in their answer, providing a command (like /bin/bash) overrides the default CMD.
In addition, -it does not imply a bash terminal. -t allocates a pseudo-tty, and -i keeps STDIN open even if not attached. See the documentation for further details.
That's an override to the default CMD specification. You can run a container with defaults, that's perfectly normal, but /bin/bash is a trick to pop open a shell so you can walk around and check out the built container to see if it's been assembled and configured correctly.

docker run with --interactive and --tty flag

Edit:
Someone mark duplicate of this question, but it does not explain the underlying mechanism at all.
But on contrast, this stack overflow solve my confusion in Case I, but not Case II.
I am newbie in docker and i am confused about the usage of --interactive, --attach flag and those concepts involved
I will show my confusion using busybox in docker hub.
Case I :
When I run a container using the below commands. docker run --interactive --tty busybox sh
A container is running and accepting input
According to the document, --interactive flag used to
Keep STDIN open even if not attached
I don't understand what is the meaning of even if not attached to, attached to what?
Case II :
Then I exit the container and try to start it using
docker start --attach abdd796820b1 .
The terminal seems accepting input as well, but when I type ls or echo, it does not give response.
What did the --attach flag do?
Please help.
There are two ways in which you can interact with a running container
attach
exec
--interactive flag
As you mentioned it already says
Keep STDIN open even if not attached
Which from my understanding means it will read inputs from your terminal/console and reacts or present output to it. If you run docker run --tty alpine /bin/sh and docker run --tty --interactive alpine /bin/sh. One with --interactive will react to it.
attach
Attach to a running process
If the docker container was started using /bin/bash command, you can access it using attach, if not then you need to execute the command to create a bash instance inside the container using exec.
More in depth: If docker container is started using /bin/bash then it becomes containers PID 1 and attach command will attach you to PID 1.
exec
Creates new process
If you want to create a new process inside container than exec it used like exec is used to execute apt-get command inside container without attaching to it or run a node or python script.
Eg: docker exec -it django-prod python migrate
See here -i is for interactive and -t is for --tty that is pseudo-TTY. Interactive so that you can enter if something is prompted by this command.
You will need to provide the -i/--interactive option to forward your terminal STDIN to the container's sh.
Try this:
docker start -ai CONTAINER
https://docs.docker.com/engine/reference/commandline/start/

Why Docker exec need --interactive? --tty should be enough?

I new to docker, when i play with docker exec. I have below question:
Command docker exec -t 26b318e534c0 bash, already have tty, this should be interactive? Why must use docker exec **-it** 26b318e534c0 bash?
You can create a pseudo tty without any input to the tty device. This allows an application generating output to do so using content recognized by the tty device (e.g. color output). If you want to be able to interactively type in that tty, then you need to pass the input option, from a terminal with tty support (not from a shell script, and not from some windows command prompts), to attach your console to the stdin of that container.
As a simple example with docker run, these two commands will look different:
docker run -t --rm debian ls -al --color=always
docker run --rm debian ls -al --color=always
The first will have color output, the second will not, and neither will allow you to type input to the ls command being run inside the container.

Question about docker run command parameters, -t -i

I am confused about these three commands, I don't know the difference among them. Sorry, I am new to docker.
I can not see the difference from the result.Could anybody tell me the difference?
docker run -it IMAGE_NAME /bin/bash
docker run -i IMAGE_NAME /bin/bash
docker run -i IMAGE_NAME
From the docker documentation
-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached
For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process.
docker run -i imagename /bin/bash
This will attach a shell to the container. You can run any shell command on the shell.
docker run -i imagename
This will dump the stdout on the terminal. Similar to docker run but with ability to take input from pipe.
Docker run command has some parameters to run command in Detached or Foreground mode.
-i and -t falls under Foreground mode.
-i : Keep STDIN open even if not attached
-t : Allocate a pseudo-tty
In case of -i whenever you run docker container command passed to it will be fired. in your case "/bin/bash"
Note from Doc
For interactive processes (like a shell), you must use -i -t together
in order to allocate a tty for the container process. -i -t is often
written -it as you’ll see in later examples. Specifying -t is
forbidden when the client is receiving its standard input from a pipe,
as in:
More Detail Here
docker run -it IMAGE_NAME /bin/bash --> you will be able to enter into container if you use -i(interactive) option (which is for executing any commands in the container) and -t(tty) which gives you the terminal to enter any command, /bin/bash is the type of linux shell (eg. sh,ksh,bash etc.)

What is the difference between docker exec -ti and docker exec -it?

I am newbie with docker, I was reading few of things and I realized some one using -it and someone -ti.
So I want to know what is the difference between docker exec -ti my_container and docker exec -it my_container.
There's no difference. -it is just a shorthand way of specifying the two flags -i and -t, as explained in the documentation:
Single character command line options can be combined, so rather than typing docker run -i -t --name test busybox sh, you can write docker run -it --name test busybox sh.
The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
Since these boolean flags can be specified in any order, -ti is equivalent to -it.
This is a very common feature of Unix-y command-line tools.

Resources