Docker Debugging: Inspecting Containers, Logs, and Common Failure Modes
Docker debugging playbook: a triage order, exit codes decoded, inspect and events, and fixes for exec format error and port clashes.

“The container keeps restarting” is the least useful bug report in containerized systems, and also the most common. Without a method, debugging turns into rebuilding the image with random changes. With a method, most issues are identified in five minutes using only the Docker CLI. What follows is that method, plus the failure modes I see over and over.
The triage order
1. Status and exit code
docker ps -a --filter name=api --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'
# api Exited (127) 4 seconds ago billing-api:1.9.0
The exit code is the first clue:
| Code | Meaning | Usual cause |
|---|---|---|
| 0 | Process finished successfully | No long-running foreground process; the command simply completed |
| 1 | Application error | Read the logs: config error, failed connection, uncaught exception |
| 125 | Docker could not run the container | Invalid flag, port already allocated, bad mount |
| 126 | Command found but not executable | Missing execute permission on a script |
| 127 | Command not found | Wrong path, missing binary, shell not present, CRLF in shebang |
| 137 | Killed with SIGKILL | Out-of-memory kill, or a stop that timed out |
| 139 | Segmentation fault | Native code crash, often a library or architecture mismatch |
| 143 | Terminated with SIGTERM | Normal graceful stop |
2. Logs
docker logs --tail 100 api # works on stopped containers too
docker logs -f --since 10m api
docker logs api 2>&1 | grep -i error
Logs survive until the container is removed, so do not delete a crashed container before reading them. If you run with --rm or Compose recreated it, the logs are gone; that is a good reason to ship logs off the host, as covered in logs and resource limits.
3. Inspect state and configuration
docker inspect api --format '{{json .State}}' | jq
# Status, ExitCode, Error, OOMKilled, StartedAt, FinishedAt, Health
docker inspect api --format '{{json .Config.Entrypoint}} {{json .Config.Cmd}}'
docker inspect api --format '{{json .Mounts}}' | jq
docker inspect api --format '{{json .NetworkSettings.Networks}}' | jq
docker inspect api --format '{{range .Config.Env}}{{println .}}{{end}}'
Checking the actual entrypoint, command, environment, mounts and networks answers “is it configured the way I think?” Mismatches between the Compose file you edited and the container that is running, because it was never recreated, show up here immediately.
4. Events
docker events --since 30m --filter container=api
Events show the sequence: create, start, die, OOM, health status changes, restart. A loop of die and start every few seconds confirms a restart policy fighting a crash.
5. Get inside, or next to it
docker exec -it api sh # running container with a shell
docker run --rm -it --entrypoint sh billing-api:1.9.0 # crashed container: start the image with a shell instead
docker cp api:/app/config/settings.yaml ./ # copy files out of a stopped container
docker run --rm -it --network container:api nicolaka/netshoot # network tools inside api's network namespace
docker run --rm -it --pid container:api busybox ps # see api's processes from a tools container
Overriding the entrypoint is the key trick for containers that crash immediately: you get a shell in the exact same image, can look at files and environment, and can run the real command by hand to see its error. For distroless images without a shell, share namespaces with a tools container as in the last two commands.
The failure modes you will meet
exec format error
The image was built for a different CPU architecture, typically arm64 on an Apple Silicon Mac and then run on an amd64 server. Check with docker image inspect --format '{{.Architecture}}' IMAGE, and build with --platform linux/amd64 or as a multi-platform image.
“no such file or directory” for a script that exists
If /docker-entrypoint.sh is clearly in the image but the container reports it missing, check the line endings. A script saved with Windows CRLF line endings has a shebang of #!/bin/sh\r, and the kernel looks for an interpreter called sh\r, which does not exist. Convert with dos2unix or sed -i 's/\r$//' in the Dockerfile, and add *.sh text eol=lf to .gitattributes. The same message appears when a binary was dynamically linked against glibc and the image is Alpine (musl): the dynamic loader it needs does not exist.
Exits immediately with code 0
A container lives only as long as its main process. Services that daemonize themselves, such as nginx without daemon off;, an init script that starts a service in the background, or a CMD that runs a setup step and finishes, exit at once. Run the service in the foreground.
The app works inside the container but not from outside
The application listens on 127.0.0.1 inside the container, which is only reachable from within that container. Make it listen on 0.0.0.0. Check with docker exec api netstat -tlnp or ss -tlnp if available, and confirm the published port mapping with docker port api.
Bind for 0.0.0.0:8080 failed: port is already allocated
Another container or a host process holds the port. docker ps --filter publish=8080 finds a container; sudo ss -tlnp | grep 8080 finds a host process.
Permission denied on a mounted directory
The container’s user ID does not have access to the files on a bind mount or volume, common after adding a non-root USER to an image. Align ownership with chown to the container’s UID, set the right UID with --user, or initialize the volume’s ownership in the image so new volumes inherit it. Details are in volumes and bind mounts.
Cannot reach another container
The containers are on different networks, or the client uses localhost. Check docker network inspect and test name resolution with docker exec api getent hosts db.
Pull failures
manifest unknown: the tag does not exist, or not for your platform.unauthorizedordenied: log in to the registry, or the CI credentials lack pull access.toomanyrequests: Docker Hub’s pull rate limit; authenticate or use a mirror.
no space left on device
Images, stopped containers, build cache, volumes and unrotated logs all share the same disk. docker system df -v shows the breakdown; prune build cache and unused images first, and fix log rotation so it does not come back.
Debugging builds
docker build --progress=plain --no-cache -t debug . # full output of every step
docker build --target build -t debug-build . # stop at an intermediate stage
docker run --rm -it debug-build sh # look around inside that stage
BuildKit’s default output collapses successful steps. --progress=plain shows everything, which is what you need when a RUN step fails with a vague message. Building only up to a stage and opening a shell in it is the build equivalent of overriding the entrypoint.
Frequently Asked Questions
Why does my Docker container exit immediately?
A container stops when its main process exits. Check the exit code and logs: code 0 usually means the command finished or a service daemonized itself into the background, while 1, 126 or 127 indicate errors or a missing or non-executable command.
How do I debug a container that crashes on startup?
Read docker logs on the stopped container, inspect its configuration, then start the same image with docker run –rm -it –entrypoint sh IMAGE to look at files and run the real command manually.
What does exec format error mean in Docker?
The image’s binaries were built for a different CPU architecture than the host, typically an arm64 image on an amd64 server. Rebuild with –platform for the target architecture or publish a multi-platform image.
How do I debug a container without a shell?
Run a tools container that shares its namespaces, for example docker run -it –network container:NAME nicolaka/netshoot or –pid container:NAME busybox, and copy files out with docker cp.
Method beats guesswork
Status, logs, inspect, events, then shell: following the same order every time turns most Docker incidents into a short investigation. Learn the exit codes, keep a tools image like netshoot handy for minimal containers, and recognize the recurring failure modes on sight. The same habits carry straight over to debugging pods in Kubernetes. For the full reference, see the docker container logs reference and the docker inspect reference. Related reading on this site: how containers differ from virtual machines.
Originally posted on 22 November 2023 and post last modified on 22 November 2023