219

I was doing some complex stuff with docker, but as turn out I don't know what -it flag means. Recently I've come across on some example of docker run command which has confused me a little.

docker run -itd ubuntu:xenial /bin/bash 

My question is what is sense to write -it flag here, if container during instantiation run bin/bash

In documentation we have an example

docker run --name test -it debian

with explanation

The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container.

and explanation for -t flag from help page

-t, --tty Allocate a pseudo-TTY

if I delete -it flag during

docker run -d ubuntu:xenial /bin/bash

my newly created container doesn't live so much

in docker ps -a

it is designated as exited

Sorry, if my question quite stupid, I can't find explanation on the Internet (I have significant misunderstanding of that point).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Alex
  • 3,923
  • 3
  • 25
  • 43
  • 1
    If `/bin/bash` has no content available on stdin, it has no commands to run and no way to prompt the user, so it exits. This is 100% normal and expected behavior. – Charles Duffy Jan 21 '18 at 16:07
  • 1
    Possible duplicate of [Confused about Docker -t option to Allocate a pseudo-TTY](https://stackoverflow.com/questions/30137135/confused-about-docker-t-option-to-allocate-a-pseudo-tty) – jdhao Sep 25 '19 at 12:42

3 Answers3

200

-it is short for --interactive + --tty. When you docker run with this command it takes you straight inside the container.

-d is short for --detach, which means you just run the container and then detach from it. Essentially, you run container in the background.

Edit: So if you run the Docker container with -itd, it runs both the -it options and detaches you from the container. As a result, your container will still be running in the background even without any default app to run.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
Fendi jatmiko
  • 2,567
  • 1
  • 9
  • 15
  • 2
    that mean there is an error in your container.. so your container is failed to start...or maybe it didnt have any `CMD` command by default,,so it didnt run any app by default. .so if you run it in the background, it would exited immediately because it didnt have any job to do. . – Fendi jatmiko Jan 21 '18 at 15:55
  • 1
    @Alex, if the program in your container is something that exits when stdin is closed, there's your answer (as to why it won't run without `-i`). Similarly, if it runs commands that behave differently based on whether there's a TTY, you can get distinct behavior depending on the presence of `-t`. – Charles Duffy Jan 21 '18 at 16:06
  • 2
    @Alex, ...and to be clear, `/bin/bash – Charles Duffy Jan 21 '18 at 16:08
  • gee.. turns out there is something similar https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately – Fendi jatmiko Jan 21 '18 at 16:34
  • -t => TTY => TeleTYpewriter [http://www.abouttty.com/](http://www.abouttty.com/) – themefield Jul 25 '19 at 17:37
  • 1
    I found adding `-it` solved the problem of my compiler in the container not outputting compiler errors in color, but I have no clue why it solves the issue, as I thought `-it` has only to do with stdin and interactive user input, nothing to do with stdout...Can anyone explain? Possibly a bug? Looks like `-it` makes gcc realize it's a proper terminal and outputs errors in color, without `-it` there's no color... – AdmiralAdama Apr 09 '20 at 14:58
  • He is not asking about -d option which is what you answered – KansaiRobot Apr 25 '20 at 04:40
  • 2
    @AdmiralAdama, what fixes the color issue is the -t option. TTY is needed to understand the color tokens. – kroiz Jul 26 '20 at 12:22
69

docker run -it ubuntu:xenial /bin/bash starts the container in the interactive mode (hence -it flag) that allows you to interact with /bin/bash of the container. That means now you will have bash session inside the container, so you can ls, mkdir, or do any bash command inside the container.

The key here is the word "interactive". If you omit the flag, the container still executes /bin/bash but exits immediately. With the flag, the container executes /bin/bash then patiently waits for your input.

dvnguyen
  • 2,954
  • 17
  • 24
  • 4
    for what I need to use -t flag then? `docker run -i ubuntu:xenial /bin/bash` – Alex Jan 21 '18 at 15:55
  • 13
    You are right. I'll take back my words. Without `-t` tag one can still interact with the container, but with it, you'll have a nicer, more features terminal. You can run with `-i` and with `-it` to see the difference. – dvnguyen Jan 21 '18 at 16:12
  • @dvnguyen what important feature is missing for example when `-i` is used alone ? I was also wondering about the difference between `-i` and `-it` – Blupon Aug 27 '22 at 08:44
7

Normal execution without any flags:

[ec2-user@ip-172-31-109-14 ~]$ sudo docker exec 69e937450dab ls
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc

If your command needs an input like cat, you can try:

[ec2-user@ip-172-31-109-14 ~]$ echo test | sudo docker exec 69e937450dab cat

Nothing will show, because there is no input stream going to the docker container. This can be achieved with the -i flag.

[ec2-user@ip-172-31-109-14 ~]$ echo test | sudo docker exec -i 69e937450dab cat
test

Now, let us suppose, you want the bash to start as process:

sudo docker exec 69e937450dab bash

You will see nothing, because the process started in the container. Adding the flag will do the deal:

[ec2-user@ip-172-31-109-14 ~]$ sudo docker exec -t 69e937450dab bash
root@69e937450dab:/# 

But this does not really help, because we need an input stream, which takes our commands and can be received by the bash. Therefore, we need to combine the two:

[ec2-user@ip-172-31-109-14 ~]$ sudo docker exec -i -t 69e937450dab bash
root@69e937450dab:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  hi  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@69e937450dab:/# 

small recap:

-t for attaching the bash process to our terminal

-i for being able to send inputs via STDIN for example with the keyboard to the bash in the container

Without -i can be used for commands, that don't need inputs. Without -t and bash can be used, when you dont want to attach the docker containers process to your shell.

David
  • 2,926
  • 1
  • 27
  • 61