Romeo Mihalcea Romeo Mihalcea - 7 months ago 75
Linux Question

Docker running a program as a daemon

I have a program to run inside docker (squid) that only performs as desired when it is running in daemon mode.

My question is, given a

entrypoint, how can I keep the program running in background without docker exiting?

Here's my (simplified) entrypoint:

set -e

# default behaviour is to launch squid
if [[ -z ${1} ]]; then
if [[ ! -d ${SQUID_CACHE_DIR}/00 ]]; then
echo "Initializing cache..."
$(which squid) -N -f /etc/squid4/squid.conf -z
echo "Starting squid..."
exec $(which squid) -f /etc/squid4/squid.conf -YCd 1
exec "$@"


Normally, when you execute a command that you want to remain running after you log out you would use the 'nohup' command as follows:

nohup [COMMAND] &

You should probably consider having squid start when the container is started. You could define this in a Dockerfile. Dockerfiles tell 'docker build' command how to create the image. This way when you start the container with 'docker run' your application will start immediately without your need for interaction with the container.