You run 'docker run my-image' but the container exits immediately. Running 'docker ps' shows nothing, and 'docker ps -a' shows the container with 'Exited (0)' status.
Docker containers need a foreground process to stay alive. If the main process exits, the container stops. Your CMD or ENTRYPOINT must run a long-lived process.
Step-by-Step Guide
Check container logs: docker logs [container-id]
Common issue: Using 'RUN' instead of 'CMD' in Dockerfile for main process
Ensure CMD runs in foreground: CMD ['npm', 'start'] not CMD ['npm', 'start', '&']
For Node.js: Use 'node app.js' not 'nodemon' (unless properly configured)
Don't use '/bin/bash' alone; add -c with a command: CMD ['/bin/bash', '-c', 'python app.py']
For debugging: docker run -it my-image /bin/bash (interactive mode)
Check if your app crashes on startup causing immediate exit
Found an issue with this solution?