How to make apache service run continuously from script file from docker?

I am trying to run apache2 service from .sh script from dockerfile. The 'sh' script contains 3 executable commands which is presented as below:

#!/bin/bash
 export DISPLAY=:0.0
 echo "pvw launcher file execution started"

 /home/data/pv/pv-5.9.0/bin/pvpython /home/data/pvw/conf/launcher.json &

 echo "pvw launcher file execution done"

 echo "Apache2 frontend exectution"
 exec apachectl -D FOREGROUND 

 echo "Starting gunicorn"
 exec python -m gunicorn --bind 172.17.0.2:9002 --keep-alive 100 --log-file temp.txt --log-level debug --timeout 600 wsgi:app

Based on this, the CMD in the dockerfile is presented as CMD ./start.sh.

Now, when I run the the docker image, the container runs till apache2 and holds the terminal there.

I want the container to execute apache2 service, run continuously and move to next command (gunicorn) in the 'sh' script and execute the gunicorn command. How should I modify the 'sh' script for the same?


Solution 1:

You're executing Apache HTTPd in the foreground, so it's doing exactly what you're asking it to do. What you want to do is daemonize Apache, so script execution can continue.

How do to this depends on the way you (or your distribution) has set up Apache, but generally the following will do:

apachectl start

instead of apachectl -D FOREGROUND.

Do take note that by daemonizing the process, you're losing the parent-child relationship with the docker process, so things like docker stop might stop working or wait until their timeout before killing Apache. Also, if Apache dies, Docker won't notice and won't automatically restart the container.

A better solution would be to have Apache and Gunicorn in separate Docker containers. If you can't do this for some reason, use some process manager like supervisord to manage the processes within the container.