Sub topic 11
Debugging Images with No Shell by Sharing Namespaces
This chapter assumes you can run docker run, docker exec, and docker logs. It also assumes you know that a Linux process runs inside namespaces, which are kernel features that limit what a process can see. You should be able to read basic ps, ss, and curl output.
Why `docker exec` Fails on Minimal Images
Many production images are built to be small. Examples are scratch, Google's distroless images, and Chainguard-style images. They contain your application and little else. There is no sh, no ps, no curl, and often no package manager.
On such an image, the usual first step fails:
docker exec -it web shThe error looks like this:
OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH: unknownYou have three bad options. You can rebuild the image with a shell, which changes what you are debugging. You can install tools into the running container, which is often impossible and is lost on restart. Or you can bring the tools from outside. This chapter covers the third option.
The Core Idea: Join the Namespaces, Not the Filesystem
A container is a normal Linux process with a restricted view of the system. The restrictions come from namespaces. The ones that matter here are:
| Namespace | What it isolates | Debugging use |
|---|---|---|
| PID | The process list | See and signal the app's processes |
| Network | Interfaces, routes, ports, DNS config | Check listening ports, test connections |
| Mount | The filesystem view | Not shared by the options in this chapter |
| UTS, IPC, user | Hostname, shared memory, user IDs | Rarely needed |
A second container can join the PID and network namespaces of the first one. The second container has its own filesystem, so it can come from an image full of tools. The target container keeps its minimal image and is not modified.
The command from the topic plan does this:
docker run --rm -it --pid=container:web --network=container:web nicolaka/netshoot--pid=container:web puts the new container in the PID namespace of web. --network=container:web puts it in the network namespace of web. nicolaka/netshoot is a public image that bundles ss, curl, dig, tcpdump, ip, and many other tools.
What You Can Check From the Tooling Container
Because the network namespace is shared, localhost inside the tooling container is localhost of web. The same interfaces, routes, and listening sockets are visible.
ss -tlnp
curl -sv http://localhost:8080/health
dig example.com
cat /etc/resolv.conf
ip addrThese answer common questions:
- Is the application listening at all, and on which address? A server bound to
127.0.0.1is reachable from the shared namespace but not from other containers. A server bound to0.0.0.0is reachable from both. - Does the health endpoint respond locally?
- Can the container resolve names?
/etc/resolv.confhere is the one from the shared network namespace's container configuration, not netshoot's own. Docker sets it up per container, so check it from inside the tooling container if resolution looks wrong. - Which routes and gateway does the container use?
Because the PID namespace is shared, ps aux lists the processes of web:
ps auxYou will see the app's process, often as PID 1 of the target, though numbering inside the shared namespace follows the target's view. This lets you confirm which command is actually running and whether child processes exist or have become zombies.
Reading the Target's Files
The tooling container has its own filesystem, so ls /app shows netshoot's /app, which does not exist. With a shared PID namespace you can reach the target's root filesystem through /proc:
ls /proc/1/root/app
cat /proc/1/root/etc/os-release
cat /proc/1/environ | tr '\0' '\n'/proc/1/root is a link to the root of the process with PID 1, which is the target's main process. /proc/1/environ holds its environment variables, separated by null bytes, so tr converts them to lines. Replace 1 with another PID if the app is not PID 1.
Reading another process's /proc entries requires permission. If you see Permission denied, the target probably runs as a different user. Add --user root to the debug container, or use --cap-add SYS_PTRACE. Some hardened setups block both, in which case use the alternatives later in this chapter.
Working Example
Suppose web is built from a distroless image. Users report timeouts, and docker logs web is empty.
- Confirm
execis not possible:
``bash docker exec -it web sh ``
- Attach the tooling container:
``bash docker run --rm -it --pid=container:web --network=container:web nicolaka/netshoot ``
- Check listeners inside it:
``bash ss -tlnp ``
Output shows 127.0.0.1:8080. The app listens only on loopback, so other containers and published ports cannot reach it. The fix is in the application configuration: bind to 0.0.0.0.
- After the change, repeat
ss -tlnpandcurl -s localhost:8080/healthto confirm.
The target was never modified, so the observation matches what production sees.
Docker Debug and Ephemeral Containers
Newer Docker Desktop and Docker Engine releases include a docker debug command that attaches a toolbox shell to a container or image. Availability depends on your Docker edition and subscription, so check docker debug --help before relying on it. The namespace-sharing command in this chapter works on any Docker Engine and has no extra requirements.
In Kubernetes the same idea is called an ephemeral container, started with kubectl debug. The principle is the same: a tools container joins the namespaces of the target.
Limits and Gotchas
- The mount namespace is not shared. You see the target's files only through
/proc/<pid>/root. Tools that expect paths such as/app/config.ymlneed the prefixed path. - The target must be running. Joining requires an existing namespace. If the container has exited, use the approach from the chapter on reproducing failed starts instead, or
docker cpanddocker diff. - Network mode matters.
--network=container:webworks whenwebuses a bridge network or the default network. Ifwebuses--network=host, you are already in the host's namespace and can run the tools directly on the host. - Restarts invalidate the join. When
webrestarts, its namespaces are recreated. The tooling container keeps pointing at the old, now dead, namespaces and must be started again. - Published ports are not visible from the tooling container. Port publishing (
-p) is configured on the host. Inside the shared namespace you test the container's own ports, such as 8080, not the host's mapped port. - Seccomp, AppArmor, and user namespaces can block
ptraceand/procaccess. If so,ssandcurlstill work, since they only need the network namespace, but/proc/<pid>/rootmay not. - Image trust.
nicolaka/netshootis a third-party public image. In regulated environments, pull it from your own registry or build your own tools image.
Alternatives
| Approach | When it fits | Cost |
|---|---|---|
| Shared namespaces with a tools image | Target is running, no shell | Needs permission to start containers |
| Debug variant of the image (for example a `:debug` tag with a shell) | You control the build | A different image from production |
| `docker cp` and `docker diff` | Target has stopped | No live view |
| `nsenter` on the host | You have root on the host | Bypasses Docker, needs the container's host PID |
Distroless projects publish :debug tags that add a BusyBox shell. This is simple but tests a slightly different image, so treat results as indicative.
Exercise
A container named api is built from a distroless image. docker exec api sh fails. You run docker run --rm -it --pid=container:api --network=container:api nicolaka/netshoot and ss -tlnp shows the app listening on 127.0.0.1:9000. Other containers get connection refused. What is the cause, and how would you read the app's environment variables from the tooling container if the app is PID 1?
Show answer
The app is bound to the loopback address only, so connections arriving on the container's network interface from other containers are refused. It must bind to 0.0.0.0, which is an application or configuration change. To read the environment, run cat /proc/1/environ | tr '\0' '\n' from the tooling container. If you get permission denied, add --user root or --cap-add SYS_PTRACE to the debug container.
Exercise
You attached a tooling container to web, and then web crashed and restarted. The tools container now shows no listening ports and an empty process list. Why, and what should you do?
Show answer
The tooling container joined the namespaces of the earlier run of web. A restart creates new namespaces, so the tooling container is attached to stale ones. Exit it and start a new tooling container with the same --pid=container:web --network=container:web options.