Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - Detached and Interactive?

Tags:

When reading different books, articles and forum threads I often saw the following Docker Run command:

docker run -tid <dockerimage> 

What I really don't understand: Does it make sense to start a detached (-d) Container interactive (-i) with pseudo tty (-t) ?

I mean a detached Container will never prompt you to a console so I don't think it's necessary to call it with -it.

like image 230
Christoph Forster Avatar asked Dec 11 '17 12:12

Christoph Forster


2 Answers

-i (interactive) is about whether to keep stdin open (some programs, like bash, use stdin and other programs don't). -d (detached) is about whether the docker run command waits for the process being run to exit. Thus, they are orthogonal and not inherently contradictory. A program like bash exits when stdin is closed, so without -i, it exits immediately.

-t (tty) allocates a pseudo-tty. You can see the difference from running bash with -it vs with just -i. For example, without -t, you don't get any prompt and ls shows results in one column. This difference is like the difference between running ls from a normal bash session and running ls | cat from a normal bash session, where cat does not have a pseudo-tty.

When you docker run bash in a container, -it and -itd behave differently as follows:

  • With -it, docker run gives you the bash prompt immediately.
  • With -itd, docker run exits immediately, but you can docker attach after that and get the bash prompt just as if you had just done docker run -it.
like image 156
Joshua Chia Avatar answered Oct 02 '22 00:10

Joshua Chia


When you run an image with only -d option, the container will exit immediately after the command executed. If you run with -itd option, the container will be detached but running in background, and you can attach back when you need. See the screenshot attached for more clarity.

enter image description here

like image 20
Anand Avatar answered Oct 02 '22 01:10

Anand