Sub topic 10
Debugging containers with no shell
This chapter assumes you can build and run images, read a Dockerfile, and use a multi-stage build. It also assumes you know what the distroless and hardened images in the earlier chapters leave out. Those images have no shell, no package manager, and often no ls or cat. That makes them harder to attack. It also means docker exec -it app sh fails.
What fails and why
Run a shell in a distroless container and you get an error like this:
docker exec -it app sh
# OCI runtime exec failed: exec: "sh": executable file not found in $PATHThe container is not broken. The image simply does not contain /bin/sh. You have three ways to debug it, and each one fits a different situation.
Option 1: Use a debug variant of the image
Distroless publishes a :debug tag for each image. It adds a busybox shell and basic tools. Use it while you troubleshoot, then switch back.
FROM gcr.io/distroless/base-debian12:debug
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Because the image contains busybox, you can open a shell. In the debug image the shell is at /busybox/sh:
docker run --rm -it --entrypoint /busybox/sh myapp:debugOverride the entrypoint with --entrypoint. Otherwise Docker starts your application instead of the shell.
This approach is simple, but it has a cost. You are debugging a different image from the one in production. A bug that depends on the exact contents of the production image can disappear.
Option 2: Attach a debug container to the running one
Docker Desktop and recent Docker versions include docker debug. It attaches a toolbox of shell and tools to a running container without changing its image.
docker debug appOn Kubernetes, ephemeral containers do the same job. The --target flag shares the process namespace of the app container, so you can see its processes and files through /proc/<pid>/root.
kubectl debug -it pod/app --image=busybox:1.36 --target=appThis is usually the best choice for a live problem. The production image stays untouched, and you bring your own tools.
Option 3: Inspect from outside
Many problems need no shell at all. These commands work on any container:
docker logs app
docker inspect app
docker cp app:/etc/app/config.yaml ./config.yaml
docker export app | tar -t | headdocker logsshows what the application wrote to stdout and stderr.docker inspectshows the environment, mounts, user, and entrypoint.docker cpcopies a file out so you can read it on the host.docker exportstreams the whole filesystem, which helps when you suspect a file is missing.
Choosing an approach
| Approach | Changes the image? | Needs a running container? | Best for |
|---|---|---|---|
| `:debug` tag | Yes, uses another image | No | Reproducing locally |
| `docker debug` or ephemeral container | No | Yes | Live production issues |
| `logs`, `inspect`, `cp` | No | Mostly | First look at any problem |
Common errors
- Forgetting
--entrypoint. The debug image starts your app, not the shell, so nothing seems to change. - Using the wrong shell path. Distroless debug images put busybox under
/busybox/, not/bin/. - Debugging as root while production runs as non-root. File permission bugs then vanish. Pass
--userto match the production user. - Running as non-root and failing to install tools. There is no package manager to fall back on, so bring tools in a separate debug container instead.
Exercise
Your Go service runs in gcr.io/distroless/static-debian12:nonroot on Kubernetes and returns errors you cannot explain from the logs. You must not restart or rebuild the pod. Which approach do you use, and why does kubectl exec -it pod/app -- sh fail?
Show answer
Use an ephemeral container with kubectl debug -it pod/app --image=busybox:1.36 --target=app. It adds a toolbox to the running pod without changing the application image or restarting it. kubectl exec ... sh fails because the distroless static image contains no /bin/sh or any other shell, so there is nothing to execute.