Interview Prep/Docker/Containers that exit, crash or misbehave

Container exits immediately with code 0 right after `docker run -d`.

BeginnerTroubleshootingContainers that exit, crash or misbehave
Answer

Likely causes → The main process finished (a shell with no TTY, a script that ends, a daemon that forks into the background).

Diagnose →

docker ps -a
docker logs c
docker inspect -f '{{.Config.Cmd}} {{.Config.Entrypoint}}' c

Fix → Run the service in the foreground (nginx -g 'daemon off;'), or use -it for shells. A container lives as long as PID 1.

Prevent → Use images whose CMD is a foreground process.

All Docker questions Roadmap: Docker & containers

More on Containers that exit, crash or misbehave