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.

Start this project - freeLive cloud workspace in your browser · lands on your portfolio
DevOps Engineerdockercontainersdockerfile

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:

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

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free

What you'll practice

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

Fix the Nginx 502 Bad GatewayDevOps projectRecover a Crashed Linux ServiceDevOps projectCorrect a Postgres Port MismatchDevOps projectDevOps roadmapStep by step to hiredDevOps interview questionsSTAR answersAll DevOps projectsProjects hub

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free
Start this project - free