Skip to content
  1. LuzineDebug
  2. Guides
  3. Read Docker container logs and debug a crashing…
Guide Beginner ≈ 12 min

Read Docker container logs and debug a crashing container

In short

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.

Be the first to approve this guide

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

  1. Find the container, even if stopped

    docker ps only shows running containers. Add -a to see stopped ones too: the STATUS column shows for example Exited (137) 2 minutes ago or Restarting (1). Note the name or ID: you will need it for every following command. A container started with --rm is deleted when it stops, and its logs go with it.

    bash
    docker ps -a
    docker ps -a --filter status=exited
  2. Read logs with docker logs

    docker logs shows the standard output and error output of the main process, even for a stopped container. -f follows live, --tail limits to the last lines, --since and --until filter by period (10m, 2h or a date), -t adds timestamps. To search for a word, redirect stderr to stdout then filter with grep on macOS and Linux, or Select-String in PowerShell.

    bash
    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
  3. Follow several services with Compose

    With Compose, docker compose logs merges the logs of every service, each line prefixed with the service name. Name one or more services to cut the noise. docker compose ps -a shows the state and exit code of each one. Running docker compose up without -d also streams logs live: handy to see in which order services start and which one fails first.

    bash
    docker compose ps -a
    docker compose logs -f --tail=50 web worker
    docker compose logs --since 10m db
  4. Read the exit code and state

    When logs are empty, the container state speaks. docker inspect gives the exit code, the OOMKilled flag and the Docker error message. Common codes: 0 normal stop, 1 application error, 125 docker run failed, 126 command not executable, 127 command not found, 137 killed by SIGKILL (memory or docker kill), 139 segmentation fault, 143 stopped cleanly by SIGTERM. In cmd.exe, replace the single quotes around the format with double quotes.

    bash
    docker inspect api --format '{{.State.ExitCode}} OOMKilled={{.State.OOMKilled}} {{.State.Error}}'
    docker inspect api --format '{{json .State}}'
    docker stats --no-stream
  5. Check the history with docker events

    docker events lists 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 an oom comes before each die.

    bash
    docker events --since 1h --filter container=api
    docker events --filter event=die --filter event=oom
  6. Get inside the container

    If the container is running, docker exec opens a shell inside it: check config files, environment variables, the database connection. Minimal images often only have sh, not bash. If the container crashes at startup, replace its command with a shell to explore it without starting the app. docker cp copies out a log file written inside.

    bash
    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
  7. Check the healthcheck

    A container can be running yet marked unhealthy: the orchestrator or a depends_on then waits forever. docker inspect shows the last probe runs with their output and exit code. In Compose, condition: service_healthy makes a service wait until its dependency is ready, which avoids connection errors at startup.

    yaml
    # 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
  8. Bonus: the same method with Kubernetes

    In Kubernetes, kubectl logs replaces docker 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 pod shows Last State, Reason (for example OOMKilled), Exit Code and the pod’s recent events.

    bash
    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

  1. Writing logs to a file: docker logs only sees stdout and stderr of the main process. Configure the app to log to standard output.
  2. Losing logs with --rm: the container and its history are deleted when it stops. Drop --rm while you investigate.
  3. Buffered Python output: messages stay in memory and vanish on crash. Set PYTHONUNBUFFERED=1.
  4. Hunting a code bug for exit code 137: it is often a memory limit, check OOMKilled and docker stats.
  5. Letting logs grow without limit: the json-file driver does not rotate by default. Set max-size and max-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

More guides