06 Running Multiple Processes - dchantzis/sfh-docker-in-development-part-1 GitHub Wiki

Source: https://serversforhackers.com/c/div-running-multiple-processes

We need to get both PH and Nginx running on this Container at the same time. We'll use Supervisord to do that.

Supervisor is a process monitor. It's capable of starting multiple processes and making sure they stay alive. So we're going to be using this to make sure we have nginx and php fpm running at the same time when the Container starts.

We'll create a new configuration file for supdervisor, named supervisord.conf to specify the running programs to monitor.

vim docker/app/supervisord.conf

and add the following lines below. Supervisor should not "daemonize" itself and run in the foreground, because the Container will run supervisor and Docker needs to see that process in the foreground.

Add the following lines in docker/app/supervisord.conf:

[supervisord]
nodaemon=true

[program:nginx]
command=nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:php-fpm]
command=php-fpm7.2
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

Then add the following inside docker/app/Dockerfile:

ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf

Rebuild the Image:

docker build -t shippingdocker/app:latest -f docker\app\Dockerfile docker\app

Run the Container with supervisord:

docker run --rm -d -v %cd%/application:/var/www/html/public -p 8080:80 shippingdocker/app:latest supervisord

Execute a command (bash) in the running Container

docker exec -it <CONTAINER_ID> bash

and check supervisor is running (inside the container):

ps aux

from inside the container we can check (using port 80):

curl localhost:80/index.php

from outside the container we can check (using port 8080):

curl localhost:8080/index.php

In the Dockerfile we can specify a command to run by default (if no other command is given), by adding the following line:

CMD ["supervisord"]

then we rebuild the Image:

docker build -t shippingdocker/app:latest -f docker/app/Dockerfile docker/app

then we run the Container without specifying a process:

docker run -d -p 8080:80 -v %cd%/application:/var/www/html/public --rm shippingdocker/app:latest

and check if everything works fine (to get the <CONTAINER_ID>, run docker ps:

docker exec -it <CONTAINER_ID> bash

and inside the container:

ps aux
curl localhost:80/index.php