troubleshooting#docker#debugging
Docker: container exits immediately with no logs
A checklist for the classic 'my container starts and dies in a second' problem.
Problem
You run `docker run my-image` and the container exits immediately. `docker ps` shows nothing; `docker ps -a` shows the container with `Exited (0)` or a non-zero code. There are no obvious logs.
Step-by-step Solution
- 1Run `docker logs <container-id>` (use `docker ps -a` to find the id). Even a crashed container keeps its stdout/stderr for a while.
- 2Add `-it` to run interactively: `docker run -it my-image` — sometimes the image's default CMD is a REPL that exits when stdin closes.
- 3Override the entrypoint and shell in: `docker run -it --entrypoint /bin/sh my-image` — you now have a shell inside the built image; run the intended command manually to see the real error.
- 4Check the `Dockerfile`'s `CMD` and `ENTRYPOINT` — a common mistake is `CMD python` with no script, which runs Python, gets no input, and exits.
- 5Confirm the app is a foreground process. Docker keeps a container alive **only as long as PID 1 is running**. Backgrounding your app with `&` or `nohup` makes the container exit immediately.
- 6For servers, ensure they bind to `0.0.0.0` — binding to `127.0.0.1` inside a container means nothing outside can reach it, but the container still runs. Not a crash, just unreachable.
- 7Add a `HEALTHCHECK` and monitor `docker inspect --format='{{json .State.Health}}' <id>` if the container starts but the app inside is unhappy.
See problem and detailed step-by-step above.
Keep reading
You may also like
troubleshooting
Kubernetes Pod stuck in CrashLoopBackOff
A methodical checklist to diagnose and fix crash-looping pods in production.
Read
troubleshooting
PostgreSQL: Too Many Connections
The classic Postgres scaling wall — and how PgBouncer solves it.
Read
troubleshooting
Nginx 502 Bad Gateway after Deploy
Nine times out of ten it's one of these five issues — here's how to isolate them.
Read
Discussion (0)
No comments yet. Be the first to weigh in.