A container is reporting unhealthy, returning 500s, sitting at 100% CPU, or just sitting there doing nothing. Restart-and-pray gets you back to working but throws away the evidence — which guarantees the next outage looks identical. This runbook walks the live diagnostic loop we use on every “this container isn’t behaving” call: what to check, in what order, and how to do it on a slim image where most debug tools don’t exist.
How to verify the state
Start with the daemon’s own view before going inside the container:
docker ps -a --filter name=<container> --format "table {{.Names}}\t{{.Status}}\t{{.RunningFor}}\t{{.Size}}"
docker inspect <container> --format '{{.State.Status}} health={{.State.Health.Status}} oom={{.State.OOMKilled}} exit={{.State.ExitCode}}'
docker stats --no-stream <container>
docker logs --tail 200 --timestamps <container>
docker top <container>
State.OOMKilled = true and ExitCode = 137 together mean the kernel killed the process for exceeding memory. Status = unhealthy means the configured health check is failing — check the health log next. docker stats tells you live CPU/memory/network/IO; sustained 100% on any of them is a clue.
What’s happening
A misbehaving container has one of a few underlying problems: it crashed and restarted (check exit code and last log lines), it ran out of memory (OOM signals in dmesg and the cgroup memory.events), it deadlocked or wedged (CPU usage drops, no log progress, requests pile up), it can’t reach a dependency (network probes fail), or its disk is full (writes fail, log noise spikes). The diagnostic loop walks each in turn.
The complication: production-shaped images are slim. There’s no ps, no netstat, no dig, often not even bash. docker exec works but lands you with whatever the image has — which for distroless images is essentially nothing. The standard pattern is to attach a debug sidecar to the same namespaces.
The procedure
- Read the logs. Always start here:
docker logs --tail 500 --timestamps <container>
docker logs --since 10m --timestamps <container>
docker logs --since 2026-06-02T08:00:00 <container> 2>&1 | grep -i error
--timestamps adds the daemon’s view of when each line arrived. Critical when correlating across services. --since accepts a duration or an ISO timestamp.
- Inspect the health-check log if a health check is configured:
docker inspect <container> --format '{{range .State.Health.Log}}{{.End}} exit={{.ExitCode}}: {{.Output}}{{"\n"}}{{end}}'
This shows the last 5 probes with their exit codes and output. A repeated Connection refused from the probe means the application’s port isn’t open; timeout means the application is reachable but slow.
- Check resource pressure with
statsand the cgroup:
docker stats --no-stream <container>
# Get the cgroup events file (cgroup v2)
CID=$(docker inspect -f '{{.Id}}' <container>)
cat /sys/fs/cgroup/system.slice/docker-${CID}.scope/memory.events
cat /sys/fs/cgroup/system.slice/docker-${CID}.scope/cpu.stat
memory.events shows OOM-kill counts. cpu.stat shows throttling — nr_throttled > 0 means the container hit its CPU limit and was throttled by the kernel.
- List processes inside the container without entering it:
docker top <container> auxf
docker top runs ps from the host but views the container’s PID namespace. Works even on distroless images that don’t have ps.
- Exec into the container if it has a shell:
docker exec -it <container> sh
# or bash, depending on the image
docker exec -it <container> /bin/bash
# Run one-off commands
docker exec <container> ls -la /var/log
docker exec <container> cat /proc/1/status
docker exec <container> df -h
exec runs a new process in the container’s namespaces. The image’s existing user, mounts, and environment apply.
- For slim/distroless images, attach a debug sidecar sharing the target container’s namespaces:
docker run --rm -it \
--pid=container:<container> \
--network=container:<container> \
--cap-add=SYS_PTRACE \
--cap-add=SYS_ADMIN \
nicolaka/netshoot
netshoot ships with dig, tcpdump, nmap, strace, lsof, iperf, and ss. --pid=container:<name> shares the target’s PID namespace, so ps aux and strace -p <pid> see the target’s processes. --network=container:<name> shares the network namespace, so tcpdump -i any captures the target’s traffic.
Useful debug-sidecar invocations:
# What's the application doing?
ps auxf
strace -p 1 -f -e trace=network 2>&1 | head -50
# What's it talking to?
ss -tunlp
tcpdump -i any -nn -c 50 'port not 22'
dig +short db.internal
nc -zv db.internal 5432
# What files is it touching?
lsof -p 1 2>/dev/null | head -50
- For a deeper view of a process — system calls, file accesses, locks — use
stracefrom the debug sidecar withSYS_PTRACE:
strace -p 1 -f -tt -e trace=file,network 2>&1 | head -200
strace -f follows forks. -tt adds microsecond timestamps. -e trace= filters to interesting syscall groups.
- Inspect the container’s actual filesystem from the host:
docker inspect <container> --format '{{.GraphDriver.Data.MergedDir}}'
# A path like /var/lib/docker/overlay2/<hash>/merged
sudo ls -la $(docker inspect <container> --format '{{.GraphDriver.Data.MergedDir}}')/app
Useful when you need to read a file the container’s cat can’t access (or the container has no cat). On overlay2, MergedDir is the same filesystem view the container sees, mounted on the host.
- Network namespace from the host.
nsenterlets you enter just the network namespace without sharing PID:
CPID=$(docker inspect -f '{{.State.Pid}}' <container>)
sudo nsenter -t $CPID -n ss -tunlp
sudo nsenter -t $CPID -n ip addr
sudo nsenter -t $CPID -n iptables -L 2>/dev/null
- Copy files in and out for offline analysis:
docker cp <container>:/var/log/app.log ./app.log
docker cp ./fix-config.yaml <container>:/etc/app/config.yaml
docker cp works even when the container is stopped, which is critical when triaging a crashed container.
- When the container is in a bad state but you don’t want to lose it, commit it to a new image before restart:
docker commit <container> debug/<name>:$(date +%s)
docker save debug/<name>:$(date +%s) | gzip > debug-snapshot.tar.gz
The committed image captures the filesystem state at that moment. You can docker run it later for forensics without re-creating the conditions.
Common pitfalls
- Restarting the container before capturing logs. The logs persist (json-file is on disk) but rolling log rotation on a crashed-then-restarted container can lose evidence. Always
docker logsfirst. - Using
docker execon a wedged container and watching exec itself hang. The daemon is still responsive; pull diagnostics from the host (namespace entry, sidecar) instead. - Running
tcpdump -i eth0from a debug sidecar and capturing nothing — the sidecar’seth0is its own, not the target’s. Use--network=container:<name>to share the namespace, thentcpdump -i any. - Mounting
/var/run/docker.sockinto a debug container “for convenience” — you’ve given that container root on the host. Use--pid=container:and--network=container:instead. - Skipping the health-check log and assuming “unhealthy” means the application crashed. The health check itself may be wrong (probing the wrong port, timing out on a slow startup) — read what the probe actually saw.
The diagnostic loop above is what we run on every “container is broken” call as part of managed operations. Captured evidence flows into a post-incident note so the same issue isn’t rediscovered six weeks later by the next on-call engineer.