Docker Labs · Lab 4 of 6

Debugging Containers: Exit Codes, Logs, Restarts and OOM Kills

Diagnose the problems you will meet in real deployments: a container that exits immediately, a restart loop, a process killed for using too much memory, and a running service you need to look inside.

Environment
Local
Level
Intermediate
Time
About 30 min
Cost
Free

You will build

A repeatable routine for "my container won't stay up": status, exit code, logs, inspect, then fix — and the ability to tell an application error from a memory kill.

Members can save this page, take notes and track progress. See membership · Sign in

Every broken container in this lab is broken on purpose, so you can see exactly what each failure looks like before you meet it at work.

Container IDs, image IDs, timestamps and sizes will differ on your machine; everything else should match.

Steps

  1. 1A container that exits straight away

    This worker refuses to start without DATABASE_URL, like most real services. -d returns immediately, so you only notice when you look at docker ps -a.

    Run
    docker run -d --name lab-crash alpine:3.24 sh -c 'if [ -z "$DATABASE_URL" ]; then echo "ERROR: DATABASE_URL is not set" >&2; exit 3; fi; echo "worker started, database: $DATABASE_URL"'
    docker ps -a --filter name=lab-crash --format 'table {{.Names}}\t{{.Status}}'
    

    Expected output

    a5ab7fad95ba95313c24616bc67c1bdaa3a5f2a3f430c35f4055e0ae6a6e4bb8
    NAMES       STATUS
    lab-crash   Exited (3) 2 seconds ago
    Check:

    Status Exited (3): the number is the process's exit code.

  2. 2Read the logs, then the state

    Logs tell you why; inspect gives the facts: exit code, whether the kernel killed it for memory, and the environment it actually got.

    Run
    docker logs lab-crash
    docker inspect lab-crash --format 'ExitCode={{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}'
    docker inspect lab-crash --format '{{json .Config.Env}}'
    

    Expected output

    ERROR: DATABASE_URL is not set
    ExitCode=3 OOMKilled=false
    ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]
    Check:

    The environment has no DATABASE_URL: that's the bug.

  3. 3Fix it and confirm

    Run
    docker rm lab-crash
    docker run --rm -e DATABASE_URL=postgres://db:5432/shop alpine:3.24 sh -c 'if [ -z "$DATABASE_URL" ]; then echo "ERROR: DATABASE_URL is not set" >&2; exit 3; fi; echo "worker started, database: $DATABASE_URL"'
    echo "exit status: $?"
    

    Expected output

    lab-crash
    worker started, database: postgres://db:5432/shop
    exit status: 0
  4. 4Watch a restart policy give up

    --restart on-failure:3 restarts a failing container at most three times. Real platforms do the same with back-off; a container that keeps restarting is a "crash loop".

    Run
    docker run -d --name lab-flaky --restart on-failure:3 alpine:3.24 sh -c 'echo "attempt at $(date +%T)"; exit 1'
    sleep 10
    docker ps -a --filter name=lab-flaky --format 'table {{.Names}}\t{{.Status}}'
    docker inspect lab-flaky --format 'RestartCount={{.RestartCount}} ExitCode={{.State.ExitCode}}'
    docker logs lab-flaky
    

    Expected output

    ...
    NAMES       STATUS
    lab-flaky   Exited (1) 10 seconds ago
    RestartCount=3 ExitCode=1
    attempt at 01:47:31
    attempt at 01:47:31
    attempt at 01:47:32
    attempt at 01:47:32
    Check:

    Four attempts in the log (the first run plus three restarts), then Docker stops trying.

  5. 5Trigger an out-of-memory kill

    This Python process allocates 16 MB at a time inside a 64 MB limit. The kernel kills it: there is no error message from the program, only exit code 137 (128 + signal 9) and OOMKilled=true.

    Run
    docker run --name lab-oom --memory 64m --memory-swap 64m python:3.14-slim python -c "
    data = []
    for i in range(1, 20):
        data.append(bytearray(16 * 1024 * 1024))
        print(f'allocated {i * 16} MB', flush=True)
    "
    echo "exit status: $?"
    docker inspect lab-oom --format 'ExitCode={{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}'
    

    Expected output

    allocated 16 MB
    allocated 32 MB
    allocated 48 MB
    exit status: 137
    ExitCode=137 OOMKilled=true
    Check:

    Exit code 137 with OOMKilled=true means raise the limit or fix the memory use — not an app bug report.

    Hint

    On Docker Desktop the numbers can differ slightly; the exit code and OOMKilled=true will match.

  6. 6Look inside a running service

    Start a healthy nginx with a memory limit, list its processes, and test its configuration without restarting it.

    Run
    docker run -d --name lab-web -p 8080:80 --memory 128m nginx:1.29-alpine
    docker exec lab-web ps
    docker exec lab-web nginx -t
    

    Expected output

    ...
    PID   USER     TIME  COMMAND
        1 root      0:00 nginx: master process nginx -g daemon off;
       30 nginx     0:00 nginx: worker process
       31 nginx     0:00 nginx: worker process
    ...
       40 root      0:00 ps
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    Check:

    PID 1 is the nginx master process; there is one worker per CPU core.

  7. 7Check resource use and configuration

    Run
    docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.PIDs}}' lab-web
    docker inspect lab-web --format '{{json .HostConfig.RestartPolicy}} Memory={{.HostConfig.Memory}}'
    

    Expected output

    NAME      CPU %     MEM USAGE / LIMIT   MEM %     PIDS
    lab-web   0.00%     17.1MiB / 128MiB    13.36%    11
    {"Name":"no","MaximumRetryCount":0} Memory=134217728
    Check:

    Memory use is well under the 128 MiB limit, and the container has no restart policy.

  8. 8Clean up

    Run
    docker rm -f lab-web lab-flaky lab-oom
    

    Expected output

    lab-web
    lab-flaky
    lab-oom

In production

On a laptop you debug with docker logs and docker exec. In production you often can't (or shouldn't) open a shell, so the evidence must already be collected: logs, exit codes, restart counts and memory metrics.

What changes

In this lab In production
docker logs on one machine Central logs with search and retention; structured (JSON) logs with request IDs
docker inspect for exit codes Platform events: ECS stopped-task reasons, Kubernetes kubectl describe pod (OOMKilled, CrashLoopBackOff)
docker stats by hand Metrics and alerts: memory near the limit, restart count rising, health checks failing
--restart on-failure:3 The orchestrator restarts with back-off and stops routing traffic to unhealthy instances
docker exec into the container Last resort, audited; slim images have no shell, so use ephemeral debug containers
Fix and re-run Fix in code or config, redeploy through CI, and add an alert so the next occurrence pages someone

A triage order that works everywhere

  1. Status and exit code. 0 = finished, 1–125 = the app failed, 126/127 = command not runnable/found, 137 = killed (usually memory), 143 = stopped with SIGTERM.
  2. Logs from the last attempt, then the one before (docker logs, kubectl logs --previous).
  3. Configuration actually received: environment, mounts, command.
  4. Resources: memory limit versus real use; CPU throttling.

Production checklist

  • Apps fail fast with a clear message when required configuration is missing
  • Logs go to stdout/stderr, ideally as JSON, and are shipped centrally
  • Every service has memory and CPU limits based on measured use
  • Alerts exist for crash loops, OOM kills and failing health checks
  • Apps handle SIGTERM and shut down cleanly within the platform's grace period
  • Runbooks list the triage steps for the team's platform

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake