What to do if a Docker container immediately exits
By Flavio
Published · Updated
Tagged: Docker
Learn what to do when a Docker container immediately exits on start: run docker logs to read the error, then remove the container and rebuild the image.
If you run a container using docker run and it immediately exits and every time you press the Start button in Docker Desktop it exits again, there is a problem.

Read the logs
The way to figure out what is wrong is to run docker logs, adding the name of the container at the end:

docker logs works on stopped containers too. You don’t need the container to be running to read what it printed before it died.
You can also click the Container name in Docker Desktop, and it will show a list of logs:

Check the exit code
docker ps only lists running containers. Add -a to see the stopped ones too, with their exit code:
docker ps -a
NAMES STATUS
node-app Exited (127) 3 seconds ago
You can also ask for it directly:
docker inspect node-app --format '{{.State.ExitCode}} {{.State.OOMKilled}}'
The exit code tells you where to look:
| Code | What it usually means |
|---|---|
0 | The main process finished without errors |
1 | The app crashed. The logs show the error, for example a missing file or an exception |
127 | The command was not found |
137 | The process was killed with SIGKILL (128 + 9). If OOMKilled is true, it ran out of memory |
I reproduced each of these with Docker 29.
Exit code 0: nothing crashed
A container runs as long as its main process runs. When that process ends, the container stops, even if everything went fine.
For example, this container exits right away with code 0:
docker run -d ubuntu:24.04
The default command of the ubuntu image is bash. With no terminal attached, bash has nothing to read, so it exits. Add -it and it stays up:
docker run -dit ubuntu:24.04
The same goes for your app. A web server must run in the foreground and keep running. If your CMD runs a script that starts something in the background and then returns, the container stops when the script returns.
My case: single quotes in CMD
In this case after a quick Google search I realized I used a single quote in my CMD command.
This is the broken Dockerfile:
FROM node:24-alpine
WORKDIR /app
COPY app.js .
CMD ['node', 'app.js']
The array form of CMD (Docker calls it the exec form) is parsed as JSON, and JSON needs double quotes. With single quotes it’s not valid JSON, so Docker treats the whole line as a shell command. You can see it in the image config:
docker inspect my-image --format '{{json .Config.Cmd}}'
["/bin/sh","-c","['node', 'app.js']"]
The shell then tries to run a program called [node,, which doesn’t exist:
/bin/sh: [node,: not found
The container exits with code 127. The fix is double quotes:
CMD ["node", "app.js"]
docker build --check flags this line. It warns “JSON arguments recommended for CMD” and points to it, without building the image.
When the command doesn’t exist at all
If the executable in exec form doesn’t exist in the image, the container never starts. docker ps -a shows it as Created, not Exited, and docker logs is empty.
The error is printed by docker run itself:
exec: "nodee": executable file not found in $PATH
Look inside the image with a shell
When the logs are not enough, start a container from the same image with a shell instead of the normal command:
docker run -it --rm --entrypoint sh my-image
Now you can check if the files are where you expect, and if the program you’re calling is installed (which node). Use bash instead of sh if the image has it. Alpine-based images like node:24-alpine don’t include bash.
Fixing it
Note that pressing Start again doesn’t help. A container keeps the image and the command it was created with, so it runs the same broken command every time.
Fix the Dockerfile, rebuild the image, remove the old container and create a new one:
docker build -t my-image .
docker rm node-app
docker run -d --name node-app my-image
You don’t need to delete the image first. Building with the same tag moves the tag to the new image. The old one stays around without a tag, and docker image prune removes it.
Want me to talk about your product? You can sponsor this site.
Related posts about docker: