LESSON · 1 OF 6

An interactive shell with -it

An interactive shell with -it

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.

Running a shell

Start a container and give it a shell as its command:

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

What -it actually means

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

Which shell to ask for

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:

bash
docker run -it ubuntu bash
docker run -it alpine sh

Commands this node introduces

  • docker run -it IMAGE sh - start a container with an interactive shell
  • -i and -t - keep input open and allocate a terminal
Spin up a fresh environment and practice live.
docker · fresh machine · ready in under a minute