Debugging containers
Sooner or later a container exits the moment it starts, a page won't load, or the disk fills up. Debugging containers follows a short routine: find out what state it's in, read what it said, look at how it was configured, then get inside if you need to.
Step 1: what state is it in?
$ docker ps -a
CONTAINER ID IMAGE STATUS NAMES
3f2a9c1b7d4e demee/app:1.1 Exited (1) 4 seconds ago web
docker ps only lists running containers; -a shows stopped ones too. The number in Exited (1) is the exit code, your first clue.
Exited (1) is your first clue. A container that keeps flipping between running and exited is crashing on start: read its logs.Step 2: read the logs
$ docker logs web # works on stopped containers too
$ docker logs --tail 50 -t web # last 50 lines, with timestamps
$ docker logs --since 10m -f web # last 10 minutes, then follow
Docker only captures what the main process writes to stdout and stderr. If your app logs to a file inside the container, docker logs stays empty; configure the app to log to the console instead.
Container exits immediately
A container lives exactly as long as its main process. Common reasons it ends straight away:
- The app crashed on startup: missing environment variable, bad config, can't reach the database. The logs say so.
- The command finished:
docker run ubuntustartsbash, which exits at once because no input is attached (no-it). That's normal, not a bug. - The process went to the background: a start script runs a daemon that forks and returns (e.g.
service nginx start). The main process must stay in the foreground:nginx -g 'daemon off;'.
To poke around in an image that won't stay up, replace its command with a shell:
$ docker run --rm -it --entrypoint sh demee/app:1.1
What the exit code means
| Code | Meaning | Typical cause |
|---|---|---|
| 0 | Finished successfully | A one-off job completed, or the command simply ended |
| 1 | General application error | Uncaught exception, failed config check: read the logs |
| 125 / 126 / 127 | Docker couldn't start it / command not executable / command not found | Wrong docker run flags, missing execute permission, typo or missing binary in CMD |
| 137 | 128 + 9: killed with SIGKILL | Out of memory, docker kill, or docker stop timed out |
| 139 | 128 + 11: segmentation fault | A crash in native code: a C library, a native module, a bug or incompatibility below your app |
| 143 | 128 + 15: stopped by SIGTERM | A normal docker stop that the app handled |
docker inspect: the full picture
docker inspect web prints everything Docker knows about a container as JSON. Use --format to pick out fields:
$ docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' web
exited exit=137 oom=true # 137 + OOMKilled: it ran out of memory
$ docker inspect --format '{{json .Mounts}}' web # what is mounted where
$ docker inspect --format '{{json .Config.Env}}' web # env vars it got
$ docker inspect --format '{{json .State.Health}}' web # healthcheck results
Getting inside, even without a shell
$ docker exec -it web sh # or bash, if the image has it
$ docker exec web env # run a single command, no shell needed
OCI runtime exec failed: ... exec: "sh": executable file not found in $PATH
That error means a minimal image (distroless, scratch) with no shell. You can still debug from outside:
$ docker top web # processes running in it
$ docker cp web:/etc/app/config.yaml . # copy a file out (works on stopped containers too)
$ docker diff web # files changed since it started
$ docker run --rm -it --network container:web nicolaka/netshoot
# a toolbox container sharing web's network: curl, dig, ss, tcpdump...
Resource usage: docker stats
$ docker stats # live CPU %, MEM USAGE / LIMIT, NET I/O, PIDS
$ docker stats --no-stream # one snapshot
$ docker run -d -m 512m --cpus 1.5 demee/app:1.1 # set limits
Memory creeping toward the limit ends in an exit code 137 with OOMKilled true.
"Port not reachable"
Work through it in order:
- Is it published?
docker port web. No output means you forgot-p. - Right container port?
-p 8080:3000only works if the app really listens on 3000. - Is the app listening on 127.0.0.1? This is the classic one. Inside a container,
127.0.0.1is the container's own loopback. Published traffic arrives on the container's network interface, so an app bound to 127.0.0.1 never sees it. Make it listen on0.0.0.0:flask run --host=0.0.0.0,python manage.py runserver 0.0.0.0:8000,vite --host.
$ docker run --rm --network container:web nicolaka/netshoot ss -tln
LISTEN 0 128 127.0.0.1:5000 0.0.0.0:* # bad: loopback only
LISTEN 0 128 0.0.0.0:5000 0.0.0.0:* # good: all interfaces
Permission denied on a bind mount
A bind mount keeps the host's file owners as plain numbers (UID/GID). If the container runs as user node (UID 1000) and the host folder belongs to UID 1001 or to root, writes fail with EACCES: permission denied.
$ docker exec web id # which UID the app runs as
$ ls -ln ./data # which UID owns the host folder
# fixes, pick one:
$ docker run --user "$(id -u):$(id -g)" -v "$(pwd)/data":/app/data ... # run as you
$ sudo chown -R 1000:1000 ./data # give the folder to the container's UID
# ...or use a named volume instead of a bind mount
On Fedora/RHEL with SELinux, you may also need to add :z to the mount (-v "$(pwd)/data":/app/data:z) so the container is allowed to access it.
Disk full: cleaning up
$ docker system df # space used by images, containers, volumes, build cache
$ docker system df -v # the same, item by item
$ docker system prune # stopped containers, unused networks,
# dangling images, unused build cache
$ docker system prune -a # also every image not used by a container
$ docker image prune -a # just images
$ docker builder prune # just build cache
docker system prune --volumes adds anonymous volumes; named volumes are only removed with docker volume prune -a or docker volume rm. Read the confirmation prompt before you type y.Quick error lookup
| Error | Fix |
|---|---|
Bind for 0.0.0.0:8080 failed: port is already allocated | Another container or program uses that host port: pick another or stop it |
Conflict. The container name "/web" is already in use | docker rm web first, or run with --rm so it's removed on exit |
pull access denied ... may require 'docker login' | Typo in the image name, or a private image: docker login |
manifest unknown | That tag doesn't exist: check the tag list on the registry |
exec format error | Wrong CPU architecture: rebuild with --platform |
no space left on device | docker system df, then prune |