Interactive mode
- Docker & Kubernetes course
- Beginner
- Lesson 31/82
- 4 min
Learn the interactive terminal with -it flags.
Interactive mode
To work with a shell inside a container, two flags are used together: -i (interactive) keeps standard input open, -t (tty) allocates a terminal. They are usually written as -it:
$ docker run -it ubuntu:24.04 bash
root@3a4b5c6d7e8f:/# ls
bin boot dev etc home ...
root@3a4b5c6d7e8f:/# exitHere the main process is bash; typing exit closes the shell and the container stops.
Detaching and reattaching
To leave an interactive container's terminal without stopping it, press Ctrl+P, Ctrl+Q. Later, docker attach reconnects to the main process. For debugging, docker exec -it, which opens a new shell, is usually safer:
docker run -dit --name kutu alpine sh # in the background with -d, but with a TTY
docker attach kutu # attach to the main process
docker exec -it kutu sh # open a separate shellPractice A temporary shell
Write the command that opens an interactive sh shell in the alpine:3.20 image and deletes the container on exit.
Starter code
# buraya yazExpected output
/ #
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens when you type exit in a bash shell opened with docker run?
- A new shell opens
- The container stops because the main process ended Correct answer
- The container keeps running in the background
- The container is removed
Explanation: Unlike a shell opened with exec, here bash is the main process.
2 Open an interactive shell.
docker run ____ ubuntu:24.04 bash
docker run -it ubuntu:24.04 bash
Explanation: -i keeps input open, -t allocates a terminal.
3 Match.
| Term | Meaning |
|---|---|
-i |
Keeps input open |
-t |
Allocates a terminal |
Ctrl+P, Ctrl+Q |
Detach without stopping |
docker attach |
Attach to the main process |