Docker - Detached and Interactive?

前端 未结 3 1364
攒了一身酷
攒了一身酷 2021-01-04 00:41

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

docker run -tid 

相关标签:
3条回答
  • 2021-01-04 01:04

    I think that in general starting up a detached interactive container like this is an unusual use case. It's certainly nothing I ever end up using in my daily use of docker.

    Some applications may behave differently when their stdout is associated with a tty vs when it's not (e.g., they may only log to stdout by default when their output is a terminal, or if you're running a shell it may simply exit when not associated with a terminal).

    You may want to docker attach to the running container. This is especially important if you've started some sort of detached shell.

    0 讨论(0)
  • 2021-01-04 01:26

    -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 orthogornal and not inherently contradictory. A program like bash exits when stdin in closed, so without -i, it exits immediately.

    -t 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.
    0 讨论(0)
  • 2021-01-04 01:27

    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.

    0 讨论(0)
提交回复
热议问题