InfraRunBook
    Back to articles

    Docker Container Exits Immediately: Exit Codes 0, 1, 125, 126, 127 and 137 Explained

    Docker
    Published: Nov 29, 2026
    Updated: Nov 29, 2026

    A troubleshooting runbook for Docker containers that start and stop right away. It explains what exit codes 0, 1, 125, 126, 127 and 137 mean, how to confirm each cause, and how to fix it.

    Docker Container Exits Immediately: Exit Codes 0, 1, 125, 126, 127 and 137 Explained

    Symptoms

    You run

    docker run -d
    or
    docker compose up -d
    , the command returns a container ID, and everything looks fine. Then you run
    docker ps
    and the container isn't there. You add
    -a
    and find it sitting in the list with a status like
    Exited (127) 3 seconds ago
    . Or it sits in a restart loop with
    Restarting (1) 5 seconds ago
    , and your monitoring on sw-infrarunbook-01 is paging you every few minutes.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker ps -a
    CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS                      NAMES
    3f2a9c1b7d44   registry.solvethenetwork.com/web:1.4   "/entrypoint.sh"   6 seconds ago    Exited (127) 4 seconds ago   web
    91be0d5a22c8   nginx:1.27       "nginx -g 'daemon of…"   2 minutes ago    Exited (0) 2 minutes ago     proxy
    

    The number in parentheses is the whole game. It's the exit code of PID 1 inside the container, or, for some codes, of the Docker client itself. Learn to read it and you'll skip most of the guesswork. The first thing I do on any dead container is pull the code and the last log lines together.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker inspect -f 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} err={{.State.Error}}' web
    exit=127 oom=false err=
    
    infrarunbook-admin@sw-infrarunbook-01:~$ docker logs --tail 20 web
    

    A quick map before we dig in. Codes 125, 126 and 127 follow shell conventions. 125 means the Docker daemon itself failed to run the container, 126 means the command was found but couldn't be executed, and 127 means it wasn't found at all. Anything above 128 means the process died from a signal, and the signal number is the code minus 128. So 137 is 128 + 9, which is SIGKILL.


    Cause 1: Exit Code 0, The Main Process Simply Finished

    Why it happens. A container lives exactly as long as its PID 1. If that process completes successfully, the container stops with code 0. Nothing is broken. I've seen this trip up people who come from VMs, where a machine stays up until you shut it down. It also bites when someone runs a service that daemonizes itself, forks into the background, and lets the foreground process exit.

    The classic case is nginx with its default

    daemon on;
    , or a start script that launches a service with
    service nginx start
    and then reaches the end of the file. The script exits cleanly, so Docker stops the container cleanly.

    How to identify it. Exit code 0, empty or very short logs, and a container that ran for a very short time.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker run -d --name proxy my-nginx-image service nginx start
    infrarunbook-admin@sw-infrarunbook-01:~$ docker ps -a --filter name=proxy
    NAMES   STATUS
    proxy   Exited (0) 1 second ago
    

    How to fix it. Run the service in the foreground. For nginx that means

    daemon off;
    . For other daemons, look for a
    --foreground
    ,
    -F
    or
    --no-daemon
    flag. If you wrap the process in a shell script, finish it with
    exec
    so the real service becomes PID 1 and receives signals properly.

    #!/bin/sh
    set -e
    # prepare config, then hand over PID 1
    exec nginx -g 'daemon off;'
    

    If the container is a one-shot job, such as a migration or a backup, exit 0 is the correct and desired result. Don't "fix" it. Just make sure your restart policy isn't

    always
    , or Docker will relaunch the finished job forever.


    Cause 2: Exit Code 1, The Application Crashed or Failed

    Why it happens. Exit code 1 is the generic "something went wrong" from the application. A missing environment variable, a bad config file, an unreachable database at startup, an unhandled exception in Python or Node. Docker isn't at fault here. Your process started fine and then decided to quit.

    How to identify it. The logs almost always tell you directly. This is the one code where

    docker logs
    is your best friend.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker logs api
    Traceback (most recent call last):
      File "/app/main.py", line 12, in <module>
        db = connect(os.environ["DATABASE_URL"])
      File "/usr/local/lib/python3.12/os.py", line 685, in __getitem__
        raise KeyError(key) from None
    KeyError: 'DATABASE_URL'
    
    infrarunbook-admin@sw-infrarunbook-01:~$ docker inspect -f '{{.State.ExitCode}}' api
    1
    

    How to fix it. Fix whatever the log complains about. In this case, pass the variable. If the logs are empty, run the container interactively with a shell override so you can poke around and launch the app by hand.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker run --rm -it --entrypoint sh registry.solvethenetwork.com/api:2.3
    /app # env | grep DATABASE
    /app # python main.py
    

    And pass the real value when you run it properly:

    infrarunbook-admin@sw-infrarunbook-01:~$ docker run -d --name api -e DATABASE_URL=postgres://app@10.0.20.15:5432/appdb registry.solvethenetwork.com/api:2.3
    

    In my experience, half of the exit 1 cases in a Compose stack are really startup ordering problems. The app boots before the database accepts connections. Add a healthcheck on the database and use

    depends_on
    with
    condition: service_healthy
    , or build retry logic into the app.


    Cause 3: Exit Code 125, The Docker Daemon Failed to Run the Container

    Why it happens. This one is different. 125 isn't from your application at all. It means the

    docker run
    command itself failed before your process ever got a chance. Typical triggers are an invalid flag, a port that's already bound on the host, a bad volume mount path, a name conflict, or a daemon-level error.

    How to identify it. You get an error straight on your terminal rather than in

    docker logs
    , and
    echo $?
    returns 125.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker run -d --name web -p 8080:80 nginx:1.27
    docker: Error response from daemon: driver failed programming external connectivity on endpoint web: Bind for 0.0.0.0:8080 failed: port is already allocated.
    infrarunbook-admin@sw-infrarunbook-01:~$ echo $?
    125
    

    Other messages I see regularly in this category:

    docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "3f2a9c1b7d44".
    docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /srv/data/web
    unknown flag: --memory-limit
    

    How to fix it. Read the message, because it's usually specific. For a port clash, find the owner and either stop it or pick another host port.

    infrarunbook-admin@sw-infrarunbook-01:~$ sudo ss -ltnp | grep ':8080'
    LISTEN 0 4096 0.0.0.0:8080 0.0.0.0:* users:(("docker-proxy",pid=2211,fd=4))
    infrarunbook-admin@sw-infrarunbook-01:~$ docker ps --filter publish=8080
    

    For a name conflict, remove the stale container with

    docker rm web
    . For a missing bind source, create the directory first with
    mkdir -p /srv/data/web
    . For bad flags, check
    docker run --help
    . If nothing obvious shows up, look at the daemon with
    journalctl -u docker --since "10 min ago"
    .


    Cause 4: Exit Code 126, Command Found but Not Executable

    Why it happens. The shell located your entrypoint or command but couldn't run it. Almost always the file lacks the execute bit, or you pointed at a directory, or the file is for the wrong architecture. I have seen this happen more often than I'd like after someone copies a script in from a Windows checkout or from a build context where file modes got flattened.

    How to identify it.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker logs web
    /bin/sh: 1: /entrypoint.sh: Permission denied
    
    infrarunbook-admin@sw-infrarunbook-01:~$ docker run --rm --entrypoint ls registry.solvethenetwork.com/web:1.4 -l /entrypoint.sh
    -rw-r--r-- 1 root root 214 Nov 28 09:14 /entrypoint.sh
    

    No

    x
    in the mode string. That's your answer. You may also see
    exec format error
    if the binary was built for arm64 and you're on an amd64 host, which is the same family of problem.

    How to fix it. Set the bit in the Dockerfile so it doesn't depend on the host's file modes.

    # Before
    COPY entrypoint.sh /entrypoint.sh
    ENTRYPOINT ["/entrypoint.sh"]
    
    # After
    COPY --chmod=0755 entrypoint.sh /entrypoint.sh
    ENTRYPOINT ["/entrypoint.sh"]
    

    On older builders without

    --chmod
    on COPY, use
    RUN chmod +x /entrypoint.sh
    . For the architecture variant, rebuild with
    docker buildx build --platform linux/amd64
    or pull the right image variant.


    Cause 5: Exit Code 127, Command Not Found

    Why it happens. The container runtime tried to run a command that doesn't exist in the image's filesystem or isn't on the

    PATH
    . Slim and distroless images are the usual culprits. You write
    CMD ["bash", "start.sh"]
    on an Alpine base that only ships
    sh
    , or you call
    curl
    in a script where it was never installed. A typo in the binary name does it too. So does a shebang pointing to an interpreter that isn't there, such as
    #!/bin/bash
    on Alpine.

    How to identify it.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker logs web
    exec /entrypoint.sh: no such file or directory
    
    infrarunbook-admin@sw-infrarunbook-01:~$ docker run --rm --entrypoint sh registry.solvethenetwork.com/web:1.4 -c 'which bash; head -1 /entrypoint.sh'
    #!/bin/bash
    

    Here

    which bash
    printed nothing, yet the script asks for
    /bin/bash
    . The confusing part is that the error says "no such file or directory" about the script, when the real missing file is the interpreter named in line one. I've lost an embarrassing amount of time on that message.

    How to fix it. Either install the missing tool or change the script to match what's present.

    # Option A: install bash on Alpine
    RUN apk add --no-cache bash
    
    # Option B: make the script POSIX and use sh
    #!/bin/sh
    

    Also check the exec form versus the shell form.

    CMD ["python", "app.py"]
    needs
    python
    on PATH, and on many images only
    python3
    exists.


    Cause 6: Windows Line Endings in Scripts (126 or 127 in Disguise)

    Why it happens. A script saved with CRLF line endings has a carriage return glued to the end of the shebang line. The kernel then looks for an interpreter literally called

    /bin/sh\r
    , which doesn't exist. You get 127 or occasionally 126 depending on the shell, and the message looks like the file is missing when it's sitting right there.

    How to identify it.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker logs web
    standard_init_linux.go:228: exec user process caused: no such file or directory
    
    infrarunbook-admin@sw-infrarunbook-01:~$ file entrypoint.sh
    entrypoint.sh: POSIX shell script, ASCII text executable, with CRLF line terminators
    

    How to fix it. Convert the file, and stop it from coming back with a

    .gitattributes
    rule.

    infrarunbook-admin@sw-infrarunbook-01:~$ sed -i 's/\r$//' entrypoint.sh
    infrarunbook-admin@sw-infrarunbook-01:~$ file entrypoint.sh
    entrypoint.sh: POSIX shell script, ASCII text executable
    
    # .gitattributes
    *.sh text eol=lf
    

    Cause 7: Exit Code 137, SIGKILL From the OOM Killer or a Forced Stop

    Why it happens. 137 is 128 + 9. Something sent SIGKILL to PID 1. There are two common senders. The first is the kernel's out-of-memory killer, which fires when the container hits its cgroup memory limit, or when the host runs dry. The second is Docker itself:

    docker stop
    sends SIGTERM, waits ten seconds by default, then sends SIGKILL if the process ignored it. A shell-form
    CMD
    is the usual reason SIGTERM gets ignored, because the shell is PID 1 and doesn't forward the signal.

    How to identify it. Check the OOM flag first. It separates the two senders cleanly.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker inspect -f 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' api
    exit=137 oom=true
    
    infrarunbook-admin@sw-infrarunbook-01:~$ sudo dmesg -T | grep -i 'killed process'
    [Sat Nov 29 08:41:07 2026] Memory cgroup out of memory: Killed process 18422 (java) total-vm:4210332kB, anon-rss:524288kB
    
    infrarunbook-admin@sw-infrarunbook-01:~$ docker stats --no-stream api
    CONTAINER ID   NAME   MEM USAGE / LIMIT   MEM %
    7c1d3e9ab210   api    511.8MiB / 512MiB   99.96%
    

    If

    oom=false
    and the container died right after a
    docker stop
    or a Compose down, it's the ten second timeout and a process that didn't handle SIGTERM.

    How to fix it. For genuine OOM, raise the limit or reduce the app's appetite. JVM apps are the repeat offenders, since older JVMs size the heap from host memory rather than the cgroup limit.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker run -d --name api --memory=1g --memory-swap=1g \
      -e JAVA_TOOL_OPTIONS='-XX:MaxRAMPercentage=70' registry.solvethenetwork.com/api:2.3
    

    For the slow-shutdown variant, switch to exec form so your app is PID 1, handle SIGTERM, and if you really need more time, raise the grace period.

    # Before (shell form, SIGTERM never reaches the app)
    CMD python app.py
    
    # After (exec form)
    CMD ["python", "app.py"]
    
    infrarunbook-admin@sw-infrarunbook-01:~$ docker stop --time 30 api
    

    A Fast Triage Routine

    When I land on a box with a dead container, I run the same four commands in order and rarely need a fifth. First

    docker ps -a
    for the code. Second
    docker inspect
    for the exit code, OOM flag and error string. Third
    docker logs --tail 50
    . Fourth, if the logs are empty, a debug run with the entrypoint replaced by a shell so I can look at the filesystem and execute things manually.

    infrarunbook-admin@sw-infrarunbook-01:~$ docker run --rm -it --entrypoint sh registry.solvethenetwork.com/web:1.4
    / # ls -l /entrypoint.sh
    / # head -1 /entrypoint.sh | od -c | head
    / # /entrypoint.sh; echo "exit=$?"
    

    That last line, running the script by hand and echoing

    $?
    , reproduces the exit code inside the container, and the code points you to the cause group above.


    Prevention

    Most of these failures are preventable before an image ever leaves your build host. Use exec-form

    ENTRYPOINT
    and
    CMD
    everywhere, and finish wrapper scripts with
    exec
    . Set file modes with
    COPY --chmod
    , and enforce LF endings for shell scripts with
    .gitattributes
    . Add a smoke test to CI that does nothing more than
    docker run --rm image --version
    or an equivalent, because a container that can't even start will fail that in seconds and you'll see 126 or 127 before production does.

    Set memory limits deliberately, measure real usage with

    docker stats
    under load, and leave headroom. Define healthchecks so orchestration knows the difference between booting and broken. Pick a restart policy that matches the workload,
    unless-stopped
    for services and
    no
    or
    on-failure:3
    for jobs, so a crash loop gets noticed rather than hidden. Ship logs off the host to your central stack so that the evidence survives when you
    docker rm
    a container. And finally, make the exit code part of your alerting: a service exiting with 0 and one exiting with 137 are different incidents, and your runbook should say so.

    Frequently Asked Questions

    What does Docker exit code 0 mean?

    Exit code 0 means the main process inside the container finished successfully. It's normal for one-shot jobs, but for a service it usually means the process daemonized or backgrounded itself instead of running in the foreground.

    What is the difference between exit codes 126 and 127?

    126 means the command was found but couldn't be executed, typically because of a missing execute permission or wrong architecture. 127 means the command or its interpreter wasn't found at all, for example bash missing from an Alpine image.

    What does Docker exit code 125 mean?

    125 means the Docker daemon failed to run the container, before your application started. Common reasons are a port already in use, a container name conflict, an invalid flag or a missing bind mount source.

    How do I tell if exit code 137 is an OOM kill?

    Run docker inspect -f '{{.State.OOMKilled}}' on the container. If it returns true, the kernel killed it for exceeding memory. If false, the container was probably force-stopped after ignoring SIGTERM for the stop timeout.

    How can I debug a container that exits before I can attach to it?

    Check docker logs and docker inspect for the exit code, then start a fresh container with --entrypoint sh and -it so you can inspect files and run the original command by hand.

    Related Articles