Sub topic 4
Interactive and detached modes
This chapter assumes you can use a terminal, know what an image and a container are, and have Docker Engine or Docker Desktop installed. You should also have run a container with docker run before.
Foreground, detached, and interactive
A container runs one main process. How docker run treats your terminal depends on the flags you give it.
- Foreground is the default. Docker attaches your terminal to the container's output. The command does not return until the main process exits.
- Detached mode (
-d) starts the container in the background. Docker prints the container ID and returns your prompt. - Interactive mode (
-i, usually with-t) keeps standard input open and allocates a pseudo-terminal, so you can type into the container.
docker run --rm hello-worldThis runs in the foreground. The container prints its message, exits, and --rm removes it.
Detached mode
Servers should usually run detached, so they keep running after you get your prompt back.
docker run -d --name web -p 8080:80 nginx:1.27The container keeps running while its main process (nginx) runs. If that process exits, the container stops. A detached container whose command finishes immediately will show as exited right away.
To see what a detached container is doing, use the commands in the next chapters:
docker ps
docker logs web
docker exec -it web shInteractive mode
Use -it when the main process needs a person to type into it, such as a shell.
-ikeeps standard input open.-tallocates a terminal, which gives you a prompt, line editing, and correct output formatting.
docker run --rm -it alpine:3.20 shYou get a shell prompt inside a new container. Typing exit ends the shell, which is the main process, so the container stops. Because of --rm, Docker then removes it.
Using -i without -t is useful when you pipe data in. Using -t without -i gives a terminal that ignores your typing, which is rarely what you want.
echo "hello" | docker run --rm -i alpine:3.20 catMode comparison
| Flags | Terminal attached | Accepts input | Typical use |
|---|---|---|---|
| none | Yes, output only | No | One-off commands |
| `-d` | No | No | Servers and databases |
| `-it` | Yes | Yes | Shells and REPLs |
| `-i` | Yes | Yes (piped) | Scripts and pipelines |
Common errors
- A detached container exits at once. Its main process finished or failed. Check
docker logs NAME. Some images, such as a barealpine, run a shell that exits immediately when there is no terminal. Add-itor give the image a long-running command. - Your terminal seems stuck. A foreground server holds the terminal. Press Ctrl+C to stop it, or start it with
-d. the input device is not a TTY. You used-twhere no terminal exists, for example in a script or CI job. Drop-t.- You exited a shell and lost your work. Leaving the main process stops the container, and files written inside it disappear if you used
--rm. Use volumes for data you need to keep.
Exercise
You run docker run -d alpine:3.20 and docker ps shows nothing. Why, and how do you keep a shell open in the container?
Show answer
The default command of alpine is a shell, and with no terminal or input attached it exits immediately, so the container stops. docker ps only lists running containers (docker ps -a would show it as exited). Start it with docker run -it alpine:3.20 sh for an interactive shell. Alternatively, run it detached with a long-running command, such as docker run -d alpine:3.20 sleep 3600.
Exercise
What is the difference between -i and -t, and when would you use -i alone?
Show answer
-i keeps standard input open, and -t allocates a pseudo-terminal. Use -i alone when you pipe data into the container, for example echo "hello" | docker run --rm -i alpine:3.20 cat, because no interactive terminal is needed.