I've used docker run -it
to launch containers interactively and docker run -d
to start them in background. These two options seemed exclusive. However, now I've noticed that docker run -dit
(or docker run -itd
) is quite common. So what is the difference? When -it
is really needed together with -d
?
Asked
Active
Viewed 2.3k times
52
-
5`-i` starts an interactive session and `-t` emulates a tty. But `-d` tells Docker to detach and run in the background. They don't really make sense together... – Dan Lowe Jan 29 '17 at 00:47
-
2@DanLowe That's why I asked. It is used on [docker site](https://docs.docker.com/engine/reference/commandline/container_update/) and elsewhere. – vehsakul Jan 29 '17 at 00:50
-
Yeah you do have a point. I thought maybe it would change behavior of `docker attach` ... but I don't see any difference. Good question. – Dan Lowe Jan 29 '17 at 01:00
1 Answers
61
Yes, sometimes, it's necessary to include -it
even you -d
When the
ENTRYPOINT
isbash
orsh
docker run -d ubuntu:14.04
will immediately stop, causebash
can't find any pseudo terminal to be allocated. You have to specify-it
so thatbash
orsh
can be allocated to a pseudo terminal.docker run -dit ubuntu:14.04
If you want to use
nano
orvim
with any container in the future, you have to specify-it
when the image starts. Otherwise you'll get error. For example,docker run --name mongodb -d mongo docker exec -it mongodb bash apt-get update apt-get install nano nano somefile
It will throw an error
Error opening terminal: unknown.

Rafaf Tahsin
- 7,652
- 4
- 28
- 45
-
I was wondering, as for the case `1.` at least, wouldn't `-dt` be enough ? – Atralb Jan 04 '21 at 10:52