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

1

Check container logs: docker logs [container-id]

2

Common issue: Using 'RUN' instead of 'CMD' in Dockerfile for main process

3

Ensure CMD runs in foreground: CMD ['npm', 'start'] not CMD ['npm', 'start', '&']

4

For Node.js: Use 'node app.js' not 'nodemon' (unless properly configured)

5

Don't use '/bin/bash' alone; add -c with a command: CMD ['/bin/bash', '-c', 'python app.py']

6

For debugging: docker run -it my-image /bin/bash (interactive mode)

7

Check if your app crashes on startup causing immediate exit

Found an issue with this solution?

Related Topics

docker port not accessibledocker build context too largedocker compose dependency order