A container that starts and stops in the same second leaves behind two pieces of evidence: an exit code and whatever it wrote before dying. Most “container won’t stay up” problems can be sorted by those two alone, before touching a Dockerfile. The cases below were reproduced with Docker Engine on Linux and a minimal BusyBox-based image, each one built to fail in a different, recognizable way.
The rule behind every exit
A container lives as long as one process
A container isn’t a small virtual machine that boots and idles. It runs one main process, and when that process ends, the container stops, whatever else was started inside it. “Exits immediately” almost always means the main process finished, crashed, or never started.
Three commands that answer most questions
docker ps -a # status and exit code
docker logs <name> # what the process printed
docker inspect <name> --format '{{.State.ExitCode}} {{.State.OOMKilled}} {{.State.Error}}'
docker ps -a includes stopped containers, which plain docker ps hides. docker inspect exposes fields the summary leaves out, including whether the kernel killed the process for using too much memory and any error Docker hit before the process ran.
The exit code table
Docker’s documentation on running containers reserves three codes for itself: “Exit code 125 indicates that the error is with Docker daemon itself,” 126 means the command “can’t be invoked,” and 127 means it “can’t be found.” Every other code, it says, is the exit code of the container’s own command. Codes above 128 follow the Unix convention of 128 plus a signal number: 137 is 128 + 9 (SIGKILL) and 143 is 128 + 15 (SIGTERM).

Exit 0: the job was done
Nothing went wrong
A container told to run sh -c 'echo "app started"; echo done' finished with:
dx-echo: Status=exited ExitCode=0
Its logs held both lines. Exit 0 means success; the command did what it was asked and returned. A container meant to stay up needs a command that doesn’t return.
The classic version: a server that backgrounds itself
Many server programs default to “daemonizing,” forking into the background and letting the launching process exit. Inside a container, that launching process is the main process. A stand-in test started a long-running process in the background and then reached the end of its script:
docker run -d --name dx-bg localbase:1 sh -c 'sleep 3600 & echo "server launched in background"'
dx-bg: Status=exited ExitCode=0
The log said “server launched in background,” and the container was already gone, taking the background process with it. The same command with the server in the foreground, exec sleep 3600, stayed running. Real servers usually have a foreground flag for exactly this, and official images set it in their default command.
No command at all
An image with no default command fails before anything runs. Starting the test image with no arguments returned Error response from daemon: no command specified and no container was created.
Exit 127 and 1: the process failed
127: wrong name or missing binary
Asking for a program the image doesn’t contain:
docker run -d --name dx-missing localbase:1 myserver --port 80
dx-missing: Status=created ExitCode=127
Error='... exec: "myserver": executable file not found in $PATH'
The status is “created,” not “exited,” because the process never started. The State.Error field in docker inspect names the problem precisely. Typos in the command, a binary missing from a slimmed-down image, and a script whose first line points to an interpreter the image lacks all end up here.
126: found, but can’t run
Exit 126 is the close cousin. Pointing the container at something that exists but can’t be executed, here the /bin directory, produced exit 126 with the error exec: "/bin": is a directory: permission denied, again with status “created.” In real images, a common cause is a shell script copied in without its execute permission.
1: the program ran and failed
A script that reads a config file that isn’t there:
dx-fail: Status=exited ExitCode=1
reading config
cat: can't open '/etc/app/config.yml': No such file or directory
Exit 1 is the generic “something went wrong” code, so the logs carry the real answer. Missing config files, missing environment variables, failed database connections and bad file permissions dominate this category. The program usually says which.
When the logs are empty
Empty logs are evidence too. docker logs dx-missing returned nothing at all, because the process never started; its StartedAt and FinishedAt timestamps in docker inspect both read 0001-01-01T00:00:00Z, Go’s zero date, meaning “never.” The failing config script, by contrast, showed a run lasting about 0.11 seconds between its start and finish times. A container with real timestamps and empty logs ran but printed nothing, which points to a program that writes to a log file inside the container instead of to its output, or one that buffers its output and died before flushing it.
Getting inside to look
When logs aren’t enough, a shell in the same image shows what the program saw. Overriding the entrypoint does it without running the failing command:
docker run --rm -it --entrypoint sh localbase:1
In the test image, ls /etc/app from that shell confirmed the directory didn’t exist. Checking files, paths and environment variables from inside settles most exit-1 cases in a minute.

Exit 137: killed from outside
Out of memory
A container limited to 16 MB of memory running a command that grows without limit:
docker run -d --name dx-oom --memory 16m --memory-swap 16m localbase:1 sh -c 'tail /dev/zero'
dx-oom: Status=exited ExitCode=137 OOMKilled=true
137 by itself only says the process got SIGKILL. OOMKilled=true says the kernel’s out-of-memory killer sent it, because the container hit its limit. docker events showed the sequence as create start oom die.
A slow, forced stop
The same 137 appears when Docker gives up waiting. According to the docker stop reference, the main process receives SIGTERM “and after a grace period, SIGKILL,” with a default of 10 seconds on Linux. Stopping a container running sleep 3600 as its main process took 10.14 seconds and ended with exit 137: the process ignored SIGTERM, and Docker killed it.
PID 1 and –init
The main process runs as PID 1, which Linux treats specially: signals with no handler installed don’t kill it. Many programs never install a SIGTERM handler because they’ve never needed one. The --init flag, described in the docker run reference as running “an init inside the container that forwards signals and reaps processes,” fixes it. With --init, the same container stopped in 0.13 seconds with exit code 143, a clean SIGTERM exit.
Restart loops
What a crash loop looks like
A restart policy can hide an exit by starting the container again. A container that exits with code 2, run with --restart on-failure:3:
dx-loop: Status=exited ExitCode=2 Restarts=3
boot at 14:58:40
boot at 14:58:41
boot at 14:58:41
boot at 14:58:42
Four boots in about two seconds: the first run plus three restarts, then Docker stopped trying. Without the :3 limit, docker ps would show the container constantly “Restarting,” and the useful information would sit in the logs, which keep every attempt.
What the documentation says about restarts
Docker’s page on starting containers automatically describes on-failure as restarting a container “if it exits due to an error, which manifests as a non-zero exit code,” and notes that a policy “only takes effect after a container starts successfully,” which it defines as being up for at least 10 seconds. In this test, Docker still made all three restart attempts for a container that died at once, so the 10-second rule shouldn’t be read as protection against a fast crash loop.
Read the first failure
docker logs shows output from every attempt in order, so the first lines after “boot” are the actual error, repeated. Fixing that error stops the loop; changing the restart policy only changes how often it repeats.

A short checklist
In order of how often it’s the answer
docker ps -afor the exit code.docker logs <name>for what the program said.- Exit 0: the command finished, so check for a backgrounding server or a one-shot command.
- Exit 125 to 127, or status “created”: check
State.Errorfor a missing binary or a bad command. - Exit 1 or another small number: read the logs, then open a shell with
--entrypoint shand look. - Exit 137: check
OOMKilled; if false, look at what stopped it and whether the process handles SIGTERM. - Restart count climbing: read the first error in the logs and fix that.
Compose works the same way
Docker Compose services are ordinary containers underneath, so docker compose ps -a and docker compose logs <service> give the same evidence. A service that exits immediately under Compose almost always exits the same way under a plain docker run with the same image and command, which is often the faster way to test.
Sources
- Running containers (exit status), Docker Docs: https://docs.docker.com/engine/containers/run/
- docker container stop, Docker Docs: https://docs.docker.com/reference/cli/docker/container/stop/
- docker container run (–init), Docker Docs: https://docs.docker.com/reference/cli/docker/container/run/
- Start containers automatically (restart policies), Docker Docs: https://docs.docker.com/engine/containers/start-containers-automatically/
