Open a Shell Inside a Running Container
Open a shell with docker exec, run one command without it and read the two errors you will meet first. After this, you can look around inside a container and know why it sometimes refuses.
To look around inside a container that is already running, use docker exec. It starts a new process in the same container, and a shell is just one more process. This page covers the command, the two errors you will meet first, and what to do when the container is not running.
Start something to look at
A container needs a long-running command, or it exits before you can get in. sleep 300 keeps this one alive for five minutes.
docker run -d --name shell-demo alpine:3.24 sleep 300
Unable to find image
Status: Downloaded newer image for alpine:3.24
-d runs it in the background, and the long ID that docker run -d prints is the container ID. You do not need it, because the name shell-demo is enough.
Open the shell
docker exec -it shell-demo sh
-i keeps your input connected and -t gives you a terminal. Together they give you a prompt like / #. Everything you type now runs inside the container. Type exit to leave. The container keeps running, because the shell was only an extra process, and the main process, sleep, is still there.
This command needs a real terminal. In the Windows Terminal or a PowerShell window it works. In a terminal inside some editors, or where the input is not a keyboard, you get "the input device is not a TTY". The next section shows how to run a single command instead.
Run one command without a shell
For a quick look you do not need to enter at all. Run the command and read the answer.
docker exec shell-demo sh -c "pwd; cat /etc/alpine-release; whoami"
/
3.24
root
You are in the root folder, the system is Alpine 3.24, and you are root inside the container. That is root inside the container, not on your computer.
Error: executable file not found
Many small images do not have bash. Alpine has sh only.
docker exec shell-demo bash
OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH
The container is fine. The program you asked for is missing from the image. Use sh instead. Images based on Debian or Ubuntu do have bash.
Error: the container is not running
docker exec works only on a running container. Stop it, and try again.
docker stop --timeout 1 shell-demo
docker exec shell-demo sh
shell-demo
Error response from daemon: container b5f687ecba35cd3df8c1279075f40f85707c3e7026ce969f4f50429f61aab80c is not running
--timeout 1 waits one second before it kills the container, instead of ten. You cannot get into a stopped container with exec. Start it again with docker start shell-demo, or read why it stopped with docker logs shell-demo.
If you do not have a running container and just want a shell in an image, start a fresh container with a shell as its command:
docker run -it --rm alpine:3.24 sh
--rm deletes it when you type exit. Nothing you did inside survives, which is the reason to use a container for experiments.
docker rm shell-demo
💡 Tip: On Linux the commands are the same. You may need sudo in front, or membership of the docker group.
âš Warning: Do not install tools or edit files inside a running container and expect them to stay. A new container from the same image starts clean. Put changes in a Dockerfile or in a volume.
Related course: Docker for Beginners.
About
Open a shell with docker exec, run one command without it and read the two errors you will meet first. After this, you can look around inside a container and know why it sometimes refuses.
- Responsible
- Jay P
- Last Update
- 08/07/2026
- Reading Time
- 3 min
Reviews
Sign in to write a reviewComments (0)
Sign in to commentNo comments yet.