So far every container ran a command and exited. Often you want to
poke around inside one interactively - open a shell, look at the
filesystem, try a few commands. That is what the -it flags are for.
Start a container and give it a shell as its command:
docker run -it alpine sh
You land at a prompt inside the container. Commands you type run inside
it - ls, cat, whoami - all against the container's own
filesystem, isolated from the host. Type exit to leave, and because
the shell was the main process, the container stops when you do.
-it is two flags combined:
-i (--interactive) keeps the input stream open, so what you type
reaches the container.-t (--tty) allocates a terminal, giving you a proper prompt with
line editing.You almost always use them together as -it when you want an
interactive session. Leave them off and the shell would start with no
way to type into it and exit immediately.
Not every image has the same shell. Small images like alpine ship
only sh, not bash. Larger ones like ubuntu and debian include
bash. If bash is not found, fall back to sh:
docker run -it ubuntu bash
docker run -it alpine sh
docker run -it IMAGE sh - start a container with an interactive
shell-i and -t - keep input open and allocate a terminal