Why Your Docker Container Exits Immediately (and How to Fix It)
A container that exits the moment you run it almost always means the foreground process failed to start. The evidence is one command away - docker logs shows you the exact error. Here's how to read it and fix the CMD.
Why containers exit immediately
Docker runs exactly one foreground process. When that process exits - for any reason - the container stops. The most common causes for instant exits are:
CMDorENTRYPOINTreferences a filename that doesn't exist in the image- The command itself returns immediately (e.g. a shell one-liner that finishes in milliseconds)
- A missing dependency causes the process to crash on startup
Find the cause with docker logs
First, see that the container stopped and grab its ID:
docker ps -a
CONTAINER ID IMAGE COMMAND STATUS NAMES
a3f2b1c4d5e6 app "python app.py" Exited (2) 3 seconds ago peaceful_ride
Then read its output:
docker logs a3f2b1c4d5e6
exec: "app.py": no such file or directory
That error message tells you exactly what went wrong - the CMD references app.py
but the file is named server.py in the image.
Fix the Dockerfile CMD
Open the Dockerfile and correct the CMD to reference the real entrypoint file:
# Wrong - file doesn't exist in the image
CMD ["python", "app.py"]
# Correct - matches the actual filename
CMD ["python", "server.py"]
Always use the exec form (["python", "server.py"]) rather than the shell form
(python server.py). The exec form runs your process as PID 1, which means it
receives OS signals (SIGTERM on docker stop) correctly - the shell form wraps it in
/bin/sh -c and signals may not reach your process.
Rebuild and verify
After fixing the CMD, rebuild and confirm the container stays up:
docker build -t app .
docker run -d -p 8080:8080 app # -d = detached (background)
docker ps # should show STATUS "Up X seconds"
curl http://localhost:8080/ # confirm the app responds
If docker ps shows the container running and curl returns a response, the fix is
complete.
Other instant-exit causes to rule out
- Wrong working directory - the
CMDruns fromWORKDIR; ifserver.pyis in/appbutWORKDIRis/, the file isn't found even with the right name. - Missing interpreter -
pythonnot installed in the image;docker logswill sayexec: "python": executable file not found in $PATH. - Script exits immediately - a shell script that runs a command and returns (no
blocking foreground process); add
execor a blocking wait at the end.
Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.
Try it in a workspace →What you'll practice
- Using docker ps -a and docker logs to diagnose a stopped container
- Correcting a wrong CMD filename and rebuilding the image
- Verifying the fix with docker run -d and curl
FAQ
Why does my Docker container exit immediately?
The foreground process in CMD or ENTRYPOINT failed to start or returned immediately. Read docker logs <container-id> to see the exact error - the most common cause is a wrong filename or path in the CMD instruction.
How do I see why a Docker container stopped?
Run docker ps -a to list stopped containers with their exit codes, then docker logs <container-id> to read the stdout/stderr output from the process. The error is almost always printed there.
What is the difference between CMD exec form and shell form in Docker?
Exec form (CMD ["python", "server.py"]) runs the process directly as PID 1 so it receives OS signals. Shell form (CMD python server.py) wraps it in /bin/sh -c, which may swallow SIGTERM on docker stop. Prefer exec form for real services.
Keep learning
Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.
Open the workspace →