Sub topic 8
Opening an interactive shell
This chapter assumes you can start containers with docker run, list them with docker ps, and use pipes and redirection in a Unix shell. You should also know that a container runs one main process. An interactive shell is a second process that you start next to it.
What an interactive shell does
docker exec starts a new process inside a running container. When that process is a shell, you get a prompt inside the container's filesystem, process list, and network. You can read configuration files, test connections, and check what the application sees.
Two flags make the shell usable:
-ikeeps stdin open, so the shell receives what you type.-tallocates a pseudo-terminal, so you get a prompt, line editing, and correct handling of Ctrl+C.
You almost always need both, written as -it.
docker run -d --name web -p 8080:80 nginx
docker exec -it web shThe prompt changes to something like # . Commands now run inside the web container. Type exit or press Ctrl+D to leave.
# nginx -t
# ls -l /var/log/nginx
# exitLeaving the shell ends only the shell process. The container keeps running because its main process, nginx, is unaffected.
Choosing a shell
The command after the container name must exist in the image. Many images, such as Debian-based ones, include bash. Minimal images such as Alpine include only sh. Distroless images and images built FROM scratch often have no shell at all.
If you get an error like executable file not found in $PATH, try the other shell:
docker exec -it web bash
docker exec -it web shsh is the safer first choice because nearly every image that has a shell has sh.
What happens without -i or -t
The flags change behavior in ways that are easy to misread.
| Command | Result |
|---|---|
| `docker exec -it web sh` | Normal interactive prompt. |
| `docker exec -i web sh` | No prompt. The shell reads commands from stdin, so you can pipe scripts in. |
| `docker exec -t web sh` | A terminal exists, but stdin is closed. The shell exits at once. |
| `docker exec web sh` | stdin is closed. The shell exits at once with no output. |
The -i without -t form is useful in scripts:
echo 'ls /etc/nginx' | docker exec -i web shWhen you pipe or redirect the output of docker exec, drop -t. A terminal merges stderr into stdout and can add carriage returns, which corrupts captured output.
Common errors
Error response from daemon: container ... is not running.execworks only on running containers. Checkdocker ps -aand readdocker logsto find out why it stopped.the input device is not a TTY. You used-twhere stdin is not a terminal, for example in a cron job or CI step. Remove-t.- Prompt works but arrow keys print odd characters. The shell was started without
-t. Add it. - Commands such as
ps,curl, orviare not found. Slim images leave out common tools. Use what the image has, or debug from outside withdocker logs.
Exercise
You run docker exec web sh in a terminal and the command returns immediately with no prompt. Why, and what is the fix?
Show answer
Without -i, stdin is not kept open, so the shell reads end-of-input and exits at once. Run docker exec -it web sh so stdin stays open and a terminal is allocated.
Exercise
You want to run a multi-line script stored on your host inside the container, with no terminal. Which flags and command do you use?
Show answer
Use -i without -t and pipe the file in, for example docker exec -i web sh < script.sh.