Sub topic 13
Debugging without a shell
This chapter assumes you can build and run containers and that you know the scratch and distroless images from earlier chapters. Those images have no /bin/sh, no ls, and no ps. When something goes wrong, you cannot run docker exec -it NAME sh. This chapter shows two ways to get a shell when you need one.
Why the usual approach fails
docker exec -it NAME sh starts a new process inside the container's filesystem. If the image has no sh binary, the command fails with an error like executable file not found in $PATH.
Adding a shell to the production image would fix the symptom, but it undoes the point of a minimal image. A shell and its tools give an attacker more to work with. A better approach is to bring the tools in only when you need them, and only for a short time.
Option 1: the distroless debug variant
Distroless publishes a :debug tag for each image. It is the same image plus a BusyBox shell and common tools. Use it to reproduce a problem locally.
# Run the debug variant, which adds a busybox shell
docker run --rm -it --entrypoint=sh gcr.io/distroless/static-debian12:debug
# On Kubernetes, attach an ephemeral debug container to a running pod
kubectl debug -it POD_NAME --image=busybox:1.36 --target=CONTAINER_NAMEThe --entrypoint=sh flag replaces the image's entrypoint, so you land in a shell instead of starting your program. To debug your own program, build the final stage from the :debug tag and run it the same way.
FROM gcr.io/distroless/static-debian12:debug-nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Run it with docker run --rm -it --entrypoint=sh IMAGE. From the shell you can run /app by hand, list files with ls, and check that the CA bundle is present.
Option 2: an ephemeral container
The debug variant needs a new container. If a pod is already misbehaving, you want to inspect that exact pod. An ephemeral container is a temporary container added to a running pod. It shares the pod's network and, with --target, the process namespace of one container.
kubectl debug creates it. The second command in the block above does this. It uses busybox:1.36 as the tool image and --target=CONTAINER_NAME to share that container's processes.
From inside, you can run ps to see your program's process and wget -qO- localhost:8080/healthz to test the endpoint. You can also read the target's files through /proc/PID/root. The PID is the process ID that ps shows for your program.
Common errors
- The shell form of
CMDfails. Shell form needs/bin/sh. Use exec form, for exampleCMD ["/app", "--port", "8080"]. --targetis missing. Without it,psin the debug container shows only its own processes.- Permission errors on
/proc/PID/root. Your program runs as a non-root user. The debug container may need to run as the same user, or you may need more privileges. - The debug tag is left in production. Pin the production Dockerfile to the non-debug tag and check this in review.
Prefer observability first
Many problems do not need a shell. Write logs to stdout, expose a health endpoint such as /healthz, and use docker logs or kubectl logs first. Reach for a debug container when logs do not explain the failure.
Exercise
Your Go service runs on gcr.io/distroless/static-debian12:nonroot in a Kubernetes pod and returns errors when calling an HTTPS API. kubectl exec POD -- sh fails. What command lets you test from inside the pod, and what flag lets you see the service's processes?
Show answer
Run kubectl debug -it POD_NAME --image=busybox:1.36 --target=CONTAINER_NAME. The --target flag shares the process namespace of the named container, so ps shows the service. The ephemeral container shares the pod network, so you can test connections from it.
Exercise
Why is adding /bin/sh to your production scratch image a poor way to make debugging easier?
Show answer
It adds a shell and tools that an attacker could use if they got into the container, and it makes the image larger. This removes the benefit of a minimal image. Use the :debug variant locally or an ephemeral container instead.