Skip to main content
Why Your Docker Container Exits Immediately (and How to Fix It)

Why Your Docker Container Exits Immediately (and How to Fix It)

KU
· Founder & Chief Editor
• 8 min read•Updated •75 views

You run the container, check docker ps, and it's already gone. Nine times out of ten it's PID 1 doing its job. Here's how to read it.

Key takeaways

  • A Docker container runs only as long as its PID 1 process.
  • If it exits immediately, your CMD finished, daemonized itself, or crashed.
  • Run docker ps -a to read the exit code (0 finished, 127 command not found, 137 killed), then docker logs for the reason, and keep the main process in the foreground.

A Docker container exits immediately because its PID 1 process ended: the command finished, forked into the background, or crashed. Read the exit code with docker ps -a, read the reason with docker logs, and keep your main process in the foreground (for nginx, daemon off;).

Verified against

Docker Engine 29.9.2, against the Docker docs for running containers (exit status), the Dockerfile reference, and docker container run and stop. Checked October 2026.

You run docker run my-image, the prompt comes back, then you check:

Bash
$ docker ps -a
CONTAINER ID   IMAGE      STATUS                     NAMES
a1b2c3d4e5f6   my-image   Exited (0) 2 seconds ago   sleepy_hopper

Gone. The confusing part is that Exited (0) means success. Nothing crashed. The container did exactly what you told it to; that just took two seconds.

The one rule that explains all of this

A container lives exactly as long as its PID 1. When you start a container, Docker runs your CMD (or ENTRYPOINT) as process ID 1 inside the namespace. When that process exits, the container stops. There is no background and no "keep it warm." So the question is never "why did my container stop?" It's "why did PID 1 finish?"

Cause 1: your main process actually finished

This is Exited (0) land. Your CMD ran a command that completes and returns.

Dockerfile
# Dockerfile
CMD ["python", "setup_db.py"]

That script sets up a schema and exits, so the container stops. Same thing if your CMD is echo hello, ls, or a one-shot migration. People hit this constantly with base images: docker run ubuntu exits instantly because the default command is bash, and with no TTY attached bash reads empty stdin, reaches EOF, and returns.

If it was supposed to be a one-shot job, it worked. If you wanted a service, you need a process that stays in the foreground until you kill it, such as CMD ["python", "app.py"] where app.py runs a blocking server loop.

Cause 2: the app daemonizes itself

This one is sneaky because the process is a "server," but it forks a background copy and the foreground one exits. PID 1 returned, so the container dies even though the daemon technically started.

nginx is the classic. By default it backgrounds itself. You have to tell it not to:

Dockerfile
# Dockerfile
CMD ["nginx", "-g", "daemon off;"]

Apache wants httpd -D FOREGROUND. Anything you'd normally run with a -d flag or a start subcommand is suspect. Docker is your process manager now; don't reach for systemd inside a container to work around this.

Cause 3: nonzero exit, meaning it crashed

Exited (1), Exited (127), Exited (137). Now it's failing, and the answer is in the logs:

Bash
$ docker logs sleepy_hopper
$ docker inspect --format '{{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}' sleepy_hopper
137 OOMKilled=true

Docker's run reference defines 125, 126, and 127; codes above 128 are 128 plus a signal number, and anything else is your process's own:

Exit codeMeaningFirst thing to check
0PID 1 finished successfullyOne-shot CMD or a daemonizing process
1, 2Generic application errordocker logs
125Error in the Docker daemon itselfBad flag, name clash, invalid option
126Command found but can't be invokedMissing execute bit on the entrypoint script
127Command not foundTypo in CMD, binary not in image, wrong PATH
137128 + 9, SIGKILLOOMKilled: true, or a docker stop timeout
139128 + 11, SIGSEGVNative crash, often an architecture or libc mismatch

Read the logs before rebuilding anything; a 127 usually names the missing binary in one line. The same signal arithmetic applies to pods, which is why our Kubernetes exit codes reference reads almost identically.

Cause 4: wrong CMD or ENTRYPOINT, or missing a TTY

Sometimes the image is fine and the invocation is wrong. If you want an interactive shell, attach one with docker run -it ubuntu bash (-i keeps stdin open, -t allocates a TTY). Without -it, bash sees no input and exits.

When you're not sure what the image even tries to run, override the entrypoint and poke around from the inside:

Bash
$ docker image inspect my-image --format 'ENTRYPOINT={{json .Config.Entrypoint}} CMD={{json .Config.Cmd}}'
$ docker run -it --entrypoint sh my-image
# now run the CMD by hand and watch it fail

This is the highest-value move on the list: you see the actual error instead of guessing at a container that's already gone.

Cause 5: shell form vs exec form, and signals

This one bites at shutdown rather than startup, but it is the same PID 1 story:

Dockerfile
# Dockerfile
CMD python app.py               # shell form: runs /bin/sh -c "python app.py"
CMD ["python", "app.py"]        # exec form: python is PID 1 directly

In shell form, PID 1 is sh, and your app is a child. Docker's Dockerfile reference says shell form runs as a subcommand of /bin/sh -c, "which does not pass signals," so your app never receives the SIGTERM from docker stop. After the default 10-second timeout the daemon sends SIGKILL, and you get Exited (137) on every shutdown. Use exec form. If your app needs to reap child processes, add docker run --init, which runs a tini-based init as PID 1.

The exact errors, and the fix for each

These come from docker run itself or from docker logs:

  • exec: "pyhton": executable file not found in $PATH (exit 127): typo in CMD, or the binary was never installed in this stage of a multi-stage build. Check with docker run --rm --entrypoint sh my-image -c 'command -v python'.
  • exec /entrypoint.sh: permission denied (exit 126): the script lacks the execute bit. Add RUN chmod +x /entrypoint.sh or COPY --chmod=755.
  • exec /entrypoint.sh: no such file or directory when the file plainly exists: the shebang interpreter is missing, usually CRLF line endings or #!/bin/bash on Alpine.
  • exec /usr/local/bin/app: exec format error: the image was built for another CPU. See fixing Docker exec format error.
  • Exited (137) with OOMKilled=true: raise --memory or fix the leak; the kernel killed it, not Docker.

The debugging loop, start to finish

Bash
$ docker ps -a                              # exit code: 0 = finished, nonzero = crashed
$ docker logs <container>                   # what it printed before dying
$ docker run --rm my-image                  # run in the foreground, watch it live
$ docker run -it --entrypoint sh my-image   # go inside, run CMD by hand

Most cases end at step two. For the wider map of Docker failures, our Docker troubleshooting guide covers the rest.

Where this breaks: with --restart always or in Compose with restart: unless-stopped, a crashing container flaps between Up and Restarting, so docker ps without -a can look healthy; check docker inspect --format '{{.RestartCount}}'. And distroless images ship no shell, so --entrypoint sh fails; rebuild from the :debug variant of the base image, which adds a busybox shell.

Questions people ask

Why does a Docker container stop when its main process exits?

Because the container is that process. Docker starts your ENTRYPOINT or CMD as PID 1 in a new namespace, and when PID 1 returns, the namespace has nothing left to run, so the container stops with that process's exit code. Background children don't count. Keep the main process in the foreground and the container stays up.

Why does docker run ubuntu exit immediately?

The ubuntu image's default command is bash. Started without a terminal, bash reads empty stdin, hits end-of-file, and exits with code 0, so the container stops at once. Run docker run -it ubuntu bash for an interactive shell, or give it a long-running command such as sleep infinity if you only need the container alive to docker exec into.

How do I keep a Docker container running?

Run a process that never returns in the foreground: your server with its foreground flag (nginx -g 'daemon off;', httpd -D FOREGROUND), declared in exec form so it is PID 1. Avoid tail -f /dev/null hacks in production images; they keep the container alive while the real service is dead, which hides failures from restart policies and health checks.

The call we'd make

Before touching the Dockerfile, run docker ps -a and docker logs. Read the exit code as a sentence: 0 means your process finished, nonzero means it broke, 137 means something killed it. If a service dies on 0, you have a backgrounding or one-shot-command problem, so pin the process to the foreground. Use exec-form CMD so signals land where they should, and keep docker run -it --entrypoint sh in your muscle memory.

Sources

Explore topics:DockerDevOps
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). Why Your Docker Container Exits Immediately (and How to Fix It). DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/fix-docker-container-exits-immediately

Markdown

[Why Your Docker Container Exits Immediately (and How to Fix It)](https://www.devopsness.com/blog/fix-docker-container-exits-immediately) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.