Sub topic 11
Debugging Images with No Shell by Sharing Namespaces
This chapter assumes you can run containers, read docker inspect output, and know that a container is a normal Linux process with its own namespaces. A namespace is a kernel feature that gives a process its own view of something, such as the process list or the network stack.
The problem
Minimal images, such as distroless or FROM scratch builds, ship only your application. They have no sh, ps, curl, or package manager. Running docker exec -it web sh fails with an error like this:
OCI runtime exec failed: exec: "sh": executable file not found in $PATHYou can't install tools into the image without rebuilding it. Rebuilding changes the thing you are trying to debug.
The idea: bring your own tools
You can start a second container that joins the namespaces of the broken one. The second container has the tools. It sees the target's processes and network as if it were running inside it.
docker run --rm -it --pid=container:web --network=container:web nicolaka/netshoot--pid=container:webshares the process namespace. Runningps auxin the tooling container lists the processes ofweb.--network=container:webshares the network namespace. The two containers have the same interfaces, IP address, and listening ports.curl localhost:8080reaches the application inweb.
What you can check
Inside the tooling container, try these commands:
ps aux
ss -tlnp
curl -sv http://localhost:8080/health
dig example.comps auxconfirms that the application is running and shows its arguments.ss -tlnpshows which ports it listens on. A common error is an application bound to127.0.0.1instead of0.0.0.0, which makes it unreachable from other containers.curltests the application the way a health check would.digtests DNS from the target's network view, using the same resolver settings.
Limits and gotchas
- The tooling container has its own filesystem. To read the target's files, use
/proc/<pid>/root/, where<pid>is the application's PID fromps. This path needs enough privileges to read the target's process, which may mean adding--cap-add=SYS_PTRACEor running as root. - Mounts are not shared. A file under
/datainwebis not at/datain the tooling container. - The target must be running. For a container that has already exited, use the entrypoint override or
docker cp, covered in other chapters. - Stopping the target removes the shared network namespace. The tooling container then loses its view.
- Only run this on hosts where you have authority to inspect the target. Sharing namespaces gives full visibility into the target's traffic and processes.
Choosing an approach
| Situation | Approach |
|---|---|
| Image has a shell and the container is running | `docker exec -it web sh` |
| Image has no shell and the container is running | Tooling container with shared namespaces |
| Container exits at startup | Override the entrypoint, or use `docker cp` |
Exercise
A distroless container named api is running, but other containers get "connection refused" when they call it on port 9000. docker exec api sh fails. Which command starts a tooling container that lets you check what api is listening on, and what would you look for in the output?
Show answer
Run docker run --rm -it --pid=container:api --network=container:api nicolaka/netshoot, then run ss -tlnp. Look for port 9000. If the application listens only on 127.0.0.1:9000, other containers cannot reach it, and the application must bind to 0.0.0.0. If port 9000 is not listed at all, the application is not listening yet or is listening on another port.