Open a Shell in a Running Container
docker ps
docker exec -it web bash
Replace web with the name or ID that docker ps shows. -i keeps standard input open and -t gives you a terminal; together they produce a normal shell prompt. Type exit or press Ctrl+D to leave. The container keeps running, because exit ends only the shell you started, not the container's main process.
Images based on Alpine and other minimal images often have no bash, and Docker then reports that the executable was not found. Use sh, which almost every image has:
docker exec -it web sh
Choose the User, Folder, and Variables
| Option | Example | What it does |
|---|---|---|
-u | -u root | Runs as root |
-u | -u 1000:1000 | Runs as a UID and GID |
-w | -w /app | Starts in this folder |
-e | -e DEBUG=1 | Sets a variable for this command |
-d | -d | Runs it in the background |
Many images run their program as an unprivileged user, and docker exec starts as that same user, so installing a package for debugging fails. docker exec -it -u root web sh opens a root shell inside the container.
Run One Command Without a Shell
docker exec web nginx -t
docker exec web cat /etc/os-release
docker exec -w /app web ls -l
For a command that needs pipes, wildcards, or the container's own variables, wrap it in sh -c with single quotes, so your shell on the host does not expand them first:
docker exec web sh -c 'ls /var/log/nginx/*.log'
When the output goes to a file on the host, leave out -t: a terminal changes line endings and mixes error messages into the output. Use -i alone when you pipe input into the command.
docker exec db sh -c 'mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" app' > app.sql
With Docker Compose
docker compose exec web sh
docker compose exec -u root web sh
docker compose exec -T db sh -c 'mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" app' > app.sql
Use the service name from compose.yaml, and run the command in the project folder. docker compose exec gives you a terminal by default; -T turns it off for scripts and redirected output. More Compose commands are in docker compose up -d.
exec, attach, and run Compared
| Command | What it does |
|---|---|
docker exec | Starts a new process in a running container |
docker attach | Connects to the main process |
docker run | Creates a new container |
docker attach connects your terminal to the input and output of the main process. Ctrl+C there goes to that process and usually stops the container. Ctrl+P followed by Ctrl+Q detaches without stopping it, but only when the container was started with -it. To watch the output, docker logs -f is the safer choice. docker run starts a separate container from the image and does not see files changed in the running one.
Changes Inside a Container Do Not Last
A package you install or a file you edit through docker exec lives in the container's writable layer. It survives a restart, but it is gone when the container is removed or replaced, and that happens on every image update, as shown in how to update a Docker container. Make lasting changes in the Dockerfile, in a bind-mounted configuration file, or in a volume.
Images Without a Shell
Distroless images and images built from scratch contain only the program, so exec sh fails. Other ways in:
- Copy files out:
docker cp web:/app/config.json .works without a shell inside. - Read the output:
docker logs webshows what the program printed. - Use a helper container: a small image that shares the network of the target lets you test connections from its point of view.
docker run -it --rm --network container:web busybox sh
Common Errors
- Container is not running: exec works only on a running container. Start it, or read
docker logsto see why it stopped. - Executable file not found: the shell or program does not exist in the image; try
sh. - The input device is not a TTY: you used
-tin a script or cron job; remove it.