what is docker run -it flag?

DockerDockerfile

Docker Problem Overview


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

Docker Solutions


Solution 1 - Docker

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

Solution 2 - Docker

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - DockerFendi jatmikoView Answer on Stackoverflow
Solution 2 - DockerdvnguyenView Answer on Stackoverflow