Read Docker container logs and debug a crashing container
docker logs -f --tail 100 my-container shows the last lines and follows the output live. If the container stopped, docker inspect gives its exit code and tells you whether it was killed for lack of memory (OOMKilled). To look inside, use docker exec -it my-container sh.
Why this method
A container that restarts in a loop or stops without warning rarely shows its error on screen. Yet Docker keeps everything the main process writes to stdout and stderr, as well as the exit state and the event history. This guide gives the order in which to query those sources: logs, exit code, events, then inspection from inside the container. The same logic applies to Kubernetes, with kubectl as a bonus at the end.
What you need
- Docker Engine or Docker Desktop with Docker Compose v2
- Access to the docker command (docker group or sudo on Linux)
- Optional: kubectl configured for your cluster
Step by step
-
Find the container, even if stopped
docker psonly shows running containers. Add-ato see stopped ones too: the STATUS column shows for exampleExited (137) 2 minutes agoorRestarting (1). Note the name or ID: you will need it for every following command. A container started with--rmis deleted when it stops, and its logs go with it.docker ps -a docker ps -a --filter status=exited -
Read logs with docker logs
docker logsshows the standard output and error output of the main process, even for a stopped container.-ffollows live,--taillimits to the last lines,--sinceand--untilfilter by period (10m,2hor a date),-tadds timestamps. To search for a word, redirect stderr to stdout then filter withgrepon macOS and Linux, orSelect-Stringin PowerShell.docker logs -f --tail 100 api docker logs --since 30m -t api # macOS / Linux docker logs api 2>&1 | grep -i error # Windows PowerShell docker logs api 2>&1 | Select-String -Pattern error -
Follow several services with Compose
With Compose,
docker compose logsmerges the logs of every service, each line prefixed with the service name. Name one or more services to cut the noise.docker compose ps -ashows the state and exit code of each one. Runningdocker compose upwithout-dalso streams logs live: handy to see in which order services start and which one fails first.docker compose ps -a docker compose logs -f --tail=50 web worker docker compose logs --since 10m db -
Read the exit code and state
When logs are empty, the container state speaks.
docker inspectgives the exit code, the OOMKilled flag and the Docker error message. Common codes: 0 normal stop, 1 application error, 125docker runfailed, 126 command not executable, 127 command not found, 137 killed by SIGKILL (memory ordocker kill), 139 segmentation fault, 143 stopped cleanly by SIGTERM. In cmd.exe, replace the single quotes around the format with double quotes.docker inspect api --format '{{.State.ExitCode}} OOMKilled={{.State.OOMKilled}} {{.State.Error}}' docker inspect api --format '{{json .State}}' docker stats --no-stream -
Check the history with docker events
docker eventslists what the daemon did: create, start,die,oom,restart, health status changes. With--since, it replays the past then keeps streaming live (Ctrl+C to quit). It is the most reliable way to count a container’s restarts and to see whether anoomcomes before eachdie.docker events --since 1h --filter container=api docker events --filter event=die --filter event=oom -
Get inside the container
If the container is running,
docker execopens a shell inside it: check config files, environment variables, the database connection. Minimal images often only havesh, notbash. If the container crashes at startup, replace its command with a shell to explore it without starting the app.docker cpcopies out a log file written inside.docker exec -it api sh docker exec api env docker run --rm -it --entrypoint sh mon-image:1.4.2 docker compose run --rm --entrypoint sh web docker cp api:/app/storage/logs/app.log ./app.log -
Check the healthcheck
A container can be running yet marked
unhealthy: the orchestrator or adepends_onthen waits forever.docker inspectshows the last probe runs with their output and exit code. In Compose,condition: service_healthymakes a service wait until its dependency is ready, which avoids connection errors at startup.# docker inspect --format '{{json .State.Health}}' db services: db: image: postgres:17 healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 3s retries: 10 web: build: . depends_on: db: condition: service_healthy -
Bonus: the same method with Kubernetes
In Kubernetes,
kubectl logsreplacesdocker logs. The key flag is--previous: in CrashLoopBackOff, the current container has just restarted and its logs are empty, while the previous instance’s logs contain the error.kubectl describe podshows Last State, Reason (for exampleOOMKilled), Exit Code and the pod’s recent events.kubectl logs api-7d9f8b6c5-x2k4q --previous kubectl logs -f deploy/api -c app --since=15m kubectl describe pod api-7d9f8b6c5-x2k4q kubectl get events --sort-by=.metadata.creationTimestamp
Mistakes to avoid
- Writing logs to a file:
docker logsonly sees stdout and stderr of the main process. Configure the app to log to standard output. - Losing logs with
--rm: the container and its history are deleted when it stops. Drop--rmwhile you investigate. - Buffered Python output: messages stay in memory and vanish on crash. Set
PYTHONUNBUFFERED=1. - Hunting a code bug for exit code 137: it is often a memory limit, check OOMKilled and
docker stats. - Letting logs grow without limit: the json-file driver does not rotate by default. Set
max-sizeandmax-file.
Checklist
Did this guide help you?
Approve it in one click: it will be highlighted for other developers.
Frequently asked questions
Where are Docker logs stored?
With the default json-file driver on Linux, in /var/lib/docker/containers/<id>/<id>-json.log. With Docker Desktop that folder lives inside the virtual machine: use docker logs.
How do I see the logs of a stopped container?
docker logs container-name works as long as the container has not been removed. Find its name with docker ps -a.
What does "exited with code 137" mean?
The process received SIGKILL: either it exceeded its memory limit (OOMKilled is true), or someone or an orchestrator killed it. Check with docker inspect.
How do I limit Docker log size?
In the compose service, add logging with driver: json-file and the options max-size: "10m" and max-file: "3", or set them globally in daemon.json.
Tools to apply this guide
Timestamp converter
Converts a Unix timestamp (seconds, milliseconds, microseconds) to a readable date in any time zone, and back.
JSON validator
Validate, format and minify JSON, with the exact line and column of the error.
Error finder
Paste an error message or a log: the tool recognises the error and opens the right fix.
More guides
- How to read a stack trace (JS, Python, PHP, Java)
- Debug JavaScript with Chrome DevTools
- Debug Node.js with breakpoints (VS Code, Chrome)
- Debug PHP with Xdebug 3 (PhpStorm, VS Code)
- Debug an Arduino with the Serial Monitor
- Find the commit that broke your code with git bisect
- How to create a minimal reproducible example