Usually we don’t. Running process in Daemon mode gives us a lot of benefits. Service can be easily managed. Remember “service XXX start”, “chkconfig XXX on”?. When terminal disconnects, the process remains.
However sometimes services just fail to start. We may want to run the process in foreground mode. To understand why? Say duplicated global envs, incorrect conf files, missing folders, access denied, etc[1]. Related reading: How To Check Linux Process Deeply With Common Sense.
In container envs, each container usually host one and only one process. One trick to lower the operation cost. When containers start, the service automatically starts. When service stops, the container stops. To achieve this, we’d like find a way running process in foreground. Then change the entrypoint to the start command.
Take redis container for example.
From:
docker run -t -d --name my-nginx nginx /bin/sh
# Start containers, then start process
docker exec -it my-nginx bash
service nginx start
To:
docker stop my-nginx; docker rm my-nginx
# When container starts, start process inside
docker run -t -d --name my-nginx nginx \
/usr/sbin/nginx -g "daemon off;"
docker exec -it my-nginx bash
ps -ef | grep nginx