Q: How do you get a shell inside a running container?
docker exec -it <container> bash or sh if bash isn't available.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>get a shell inside a running container</strong>, run <code>docker exec -it <container> sh</code> (or <code>bash</code> when the image ships it). <code>exec</code> starts a new process in the container's existing namespaces, so leaving the shell does not stop the container. Interviewers ask this to check you know the difference from <code>docker attach</code> and <code>docker run</code>, and what to do when there is no shell at all, as in distroless or scratch images. Strong answers cover debugging safely without changing production state.
Mental model: exec starts a second process
A container is just processes isolated by namespaces and cgroups. docker exec joins those namespaces with a new process.
- exec: spawns an additional process (your shell) inside a running container. Exiting it does not affect the main process.
- attach: connects your terminal to the container's main process (PID 1) stdin/stdout. Pressing Ctrl+C or exiting can stop the container, so it is rarely what you want for debugging.
- run: creates a brand-new container.
docker run -it image shis for exploring an image, not the live instance. - Flags:
-ikeeps stdin open,-tallocates a TTY. Use both for interactive shells. - Choosing the shell: to learn what is available, try
docker exec my-app sh -c 'echo $0; ls /bin'or inspect the image withdocker image inspectfor its default command and user.
Hands-on: get a shell inside a running container
Pick the shell that exists in the image and the user you need.
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}'
docker exec -it my-app sh # BusyBox/Alpine
docker exec -it my-app bash # Debian/Ubuntu based
docker exec -it -u 0 -w /app my-app sh # root, in /app
docker exec my-app cat /etc/os-release # one-off, no TTY needed
docker compose exec web sh
- Alpine and BusyBox images: only
shexists by default, sobashfails withexecutable file not found. - Run as root:
--user 0(or-u root) helps when the app user cannot install or inspect things. Use it sparingly on production. - Set context:
-w /appsets the working directory and-e KEY=valueadds a variable for that session only. - Compose:
docker compose exec web shtargets a service by name without finding the container name. - Environment differences: an exec session does not run the container's entrypoint script, so variables set by that script may be missing. Use
docker exec my-app envand compare with the application's own view.
When the image has no shell
Distroless and scratch images deliberately omit shells and package managers, so exec sh fails.
# Debug sidecar sharing the target's PID and network namespaces
docker run --rm -it \
--pid=container:my-app --net=container:my-app \
nicolaka/netshoot sh
# ps aux -> shows the app's processes
# ss -tlnp -> shows its listening sockets
- Share namespaces from a debug container: start a tooling image attached to the target's PID and network namespaces.
- Docker Debug:
docker debug <container>is available in recent Docker Desktop and CLI releases (check your subscription and version) and injects a toolbox without modifying the image. - Kubernetes equivalent:
kubectl debug -it pod/my-app --image=busybox:1.36 --target=appadds an ephemeral container. - Copy files out instead: sometimes
docker cpand reading logs is all you need. - Non-interactive use: in scripts drop
-tand use-ionly when piping input. A TTY in CI logs produces carriage returns and breaks automation.
Verify and handle common errors
Read the error text, because each one points at a different cause.
- Container is not running:
docker execonly works on running containers. Checkdocker ps -aanddocker logs. For a crashed one, start a copy withdocker run -it --entrypoint sh image. - OCI runtime exec failed: usually means the binary (
bash) is not in the image. Trysh. - Confirm where you are: inside the shell,
hostnameequals the container ID andpsshows few processes, proving you are in the container, not the host. - Garbled terminal: missing
-tcauses odd line handling. Resize problems often fix withstty rows 40 cols 120. - Exited containers: to look inside a crashed container's files, either
docker cpout the paths you need or create an image from it withdocker commitand run that image with an overridden entrypoint.
Production practice: debug without drift
Shell access in production is a change surface, so treat it that way.
- Immutable mindset: do not install tools or edit files in a live container. They vanish on redeploy and hide the true state.
- Audit access: limit who can reach the Docker socket or
kubectl exec, and log exec events through audit tooling. - Prefer observability: logs, metrics and traces answer most questions, leaving exec for last-resort inspection.
- Remote hosts: use a Docker context or SSH to reach the daemon, for example
docker --context prod exec -it my-app sh, rather than sharing the daemon port unauthenticated over the network.
- I run <code>docker exec -it container sh</code>, which starts a new process in the running container's namespaces and leaves the main process untouched.
- If bash is missing I use sh, and for distroless images I attach a debug container with <code>--pid</code> and <code>--net</code>, or use <code>kubectl debug</code> in Kubernetes.
- I treat the session as read-only inspection and move fixes into the image or manifest.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Using docker attach to get a shell It connects to PID 1, so exiting or Ctrl+C can stop the container.
- Assuming bash exists in every image Alpine and many slim images only have sh, producing an executable-not-found error.
- Installing debug tools in a live container Changes are lost on recreate and make the running state diverge from the image.
- Forgetting that exec needs a running container A crash-looping container cannot be exec'd into. Inspect logs or run the image with an overridden entrypoint instead.