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).

2

3 Answers 3

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.

8
  • 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. . Jan 21, 2018 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. Jan 21, 2018 at 16:06
  • 2
    @Alex, ...and to be clear, /bin/bash </dev/null exits immediately too, if you run it without Docker. Running docker run without -i is doing the same thing to the copy of bash that it starts. (Without -t, it has a stdin but not a TTY, so it doesn't detect itself as an interactive shell, so you get a slightly different set of behaviors). Jan 21, 2018 at 16:08
  • 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... Apr 9, 2020 at 14:58
  • 2
    @AdmiralAdama, what fixes the color issue is the -t option. TTY is needed to understand the color tokens.
    – kroiz
    Jul 26, 2020 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.

3
  • 4
    for what I need to use -t flag then? docker run -i ubuntu:xenial /bin/bash
    – Alex
    Jan 21, 2018 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, 2018 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, 2022 at 8: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.