Home/Engineer/Troubleshooting/When Docker Containers Keep …
TroubleshootingBeginnerUbuntuDebianCentOSDockerRestartingExited

When Docker Containers Keep Restarting — Causes and Fixes by Exit Code

A guide to diagnosing and fixing containers that repeatedly show Restarting or Exited in docker ps by checking logs, exit codes, and the restart policy.

Contents

    Primary sources

    Decision table: Exit codes, causes, and fixes

    Exit codeMeaningTypical causeFix
    0Normal exitThe container finished its work (intentional exit)No action needed
    1Application errorApplication runtime errorReview logs; check env vars and ports (docker logs, docker inspect)
    137SIGKILLOOM Killer or docker killCheck memory usage and raise the limit (docker stats, docker run -m 512m, docker-compose.yml mem_limit: 512m)
    139SegfaultApplication memory corruptionDebug the code and improve memory handling
    143SIGTERMClean shutdown after docker stopNo action needed

    Step 1 — Check status and exit code

    Bash
    # All container statuses (including stopped)
    docker ps -a
    
    # Example output
    # CONTAINER ID  IMAGE    STATUS                     NAMES
    # abc123        myapp    Restarting (1) 5s ago      myapp
    # def456        nginx    Exited (137) 2 minutes ago nginx
    #                        ↑ exit code

    Step 2 — Check container logs

    Bash
    # Last 100 lines of logs
    docker logs --tail 100 container-name
    
    # Follow logs in real time (useful while restarts are looping)
    docker logs -f container-name
    
    # Include timestamps
    docker logs -t --tail 50 container-name
    
    # Works for stopped containers too
    docker logs container-id

    Exit code 1 — Application crash

    Check the logs for error messages.

    Bash
    docker logs container-name 2>&1 | tail -30

    Common causes:

    • Missing environment variable: Error: DATABASE_URL is not defined
    • Port conflict: Error: listen EADDRINUSE :::3000
    • Missing file/config: No such file or directory
    Bash
    # Check environment variables
    docker inspect container-name | grep -A 20 '"Env"'
    
    # Inspect inside a one-off container
    docker run --rm -it --env-file .env image-name /bin/sh

    Exit code 137 — OOM kill

    Bash
    # Check whether it was OOM-killed
    docker inspect container-name | grep -i oom
    # "OOMKilled": true means OOM
    
    # Check memory usage
    docker stats --no-stream container-name
    Bash
    # Increase the memory limit (docker run)
    docker run -m 512m image-name
    
    # docker-compose.yml
    # services:
    #   myapp:
    #     mem_limit: 512m

    Check and change the restart policy

    Bash
    # Check the current restart policy
    docker inspect container-name | grep -A 3 'RestartPolicy'
    PolicyBehavior
    noDo not restart (default)
    alwaysAlways restart (regardless of exit code)
    on-failureRestart only on non-zero exit
    unless-stoppedAlways restart until manually stopped
    Bash
    # If restart loops are the problem, switch the policy to on-failure
    docker update --restart=on-failure:5 container-name
    # Stop after 5 attempts
    
    # Stop completely
    docker update --restart=no container-name
    docker stop container-name

    Debugging — start the container manually

    Bash
    # Ignore the entrypoint and drop into a shell (inspect the cause directly)
    docker run --rm -it --entrypoint /bin/sh image-name
    
    # Enter a shell with the existing container's env
    docker run --rm -it   $(docker inspect container-name --format '{{range .Config.Env}}-e {{.}} {{end}}')   --entrypoint /bin/sh   image-name

    FAQ

    Q. docker ps keeps showing Restarting. Where should I start? A. Start with docker logs <container> to see the output just before exit. Then get the exit code with docker inspect <container> --format '{{.State.ExitCode}}'. 137 = OOM (out of memory), 1 or 2 = application exception, 126 or 127 = missing executable or a permission problem. If the restart policy is always, the container will loop forever every time the app dies, so switch to --restart=no first so you can capture logs.

    #Docker#Restarting#Exited#exit code#Troubleshooting#restart policy#container
    Editorial note

    AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

    Topic-related official docs (auto-linked · not per-claim sources)Docker documentation ↗

    Questions & answers

    Ask a question about this guide. We'll review and reply.