Why does my container immediately exits?
Containers are not like virtual machines
Each docker contain have a main process that is run via CMD / ENTRYPOINT command in the Dockerfile. Once those processes finishes and exit then the container will stop and exit as well!
It will not run indefinitely. Images like NGINX will run forever because it has a foreground process that is kept on listening for connections. If you take a look at the ENTRYPOINT command for NGINX image, it will run the nginx program with daemon off, so it is running in the foreground as the main process.
Images like php or ubuntu will exit immediately because the CMD that it executes by default is /bin/bash or php interpreters. And you didn't allocate an interactive shell via --interactive and -tty. Then it has no STDIN from the user, and therefore the /bin/bash just exits and thus container exits as well.
Therefore if you want to keep your php or ubuntu running, use -it and -d, if you aren't changing the default command, since the default command that is run is their interpreter.
For my use case I would change the CMD to start the PocketMine-MP server, and thus using -it isn't needed because the server will run indefinitely just like nginx. I would use -d to not see any of the outputs.
No Comments