You run docker run myimage or docker compose up and the container vanishes instantly. docker ps shows nothing. docker ps -a shows the container with status Exited (0) or Exited (1) seconds ago.
Here’s how to figure out what’s happening and fix it.
Step 1: Check the logs first
Before anything else:
| |
If the container is already gone, use the container ID from docker ps -a:
| |
This will usually tell you exactly what went wrong.
Cause 1: No foreground process (Exit 0)
The most common cause — especially with custom images. Docker containers exit when their main process exits. If your CMD or ENTRYPOINT runs a script that completes immediately, the container exits cleanly with code 0.
Classic example: You wrote a Dockerfile that runs a setup script but forgot to start the actual service:
| |
The setup script runs, finishes, container exits.
Fix: Make sure your CMD starts a long-running foreground process:
| |
The key is daemon off; — without it, nginx forks to the background and the foreground process exits immediately.
Same issue with other services:
| |
Cause 2: The process crashes on startup (Exit 1 or other non-zero)
If the logs show an error, you have a crashing process. Common culprits:
Missing environment variables:
Fix: pass the variable with -e or in your compose file:
| |
Port already in use:
Fix: stop whatever is using the port, or map to a different host port:
| |
File or volume not found:
Fix: make sure the file exists in the image or mount it correctly:
| |
Cause 3: Shell vs exec form in CMD
There’s a subtle difference between these two CMD forms:
| |
The shell form can cause issues with signal handling — Docker can’t send SIGTERM to the actual process because it goes to the shell wrapper instead. In some cases this causes unexpected exits.
Always prefer exec form:
| |
Cause 4: Script exits on error
If your entrypoint is a shell script, set -e at the top causes it to exit on any command that returns non-zero — including things like grep finding no matches, or a curl that fails.
| |
Fix: Either remove set -e or handle the specific commands:
| |
Cause 5: Wrong architecture
If you pulled an image built for a different architecture (e.g., arm64 image on amd64 host), it may exit immediately with a cryptic error:
Fix: specify the platform when pulling or building:
| |
Debugging trick: override the entrypoint
If the container exits before you can see what’s happening, override the entrypoint to drop into a shell:
| |
Now you’re inside the container and can run commands manually to see what fails.
If even that exits immediately, the base image probably doesn’t have a shell — use a debug sidecar:
| |
Quick diagnosis checklist
docker logs <id>— read the actual error- Exit code 0 → no foreground process, check your CMD
- Exit code 1+ → process crashed, check logs for the specific error
- Override entrypoint with
bashto debug interactively - Check environment variables, volume mounts, port conflicts
- Verify architecture matches your host
The logs almost always have the answer. Start there.