Nginx – PHP7-FPM Docker CMD results in 502 Nginx Error

crondockerdocker-composenginxphp-fpm

I've a docker compose that use two containers: nginx and php7-fpm.
I'm building php via Dockerfile and I need cron installed on it:

FROM 'php:7-fpm'
# PDO
RUN docker-php-ext-install pdo_mysql
# Cron
RUN apt-get update -q -q && apt-get install -y cron
ADD crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log // this line is breaking things

This way, nginx report a 502 Bad Gateway error. If i remove the last line, CMD, everything works fine. How can I fix this?

Best Answer

In docker container, you can only have one entrypoint or a command executed when you start the container. When you have at the end of your Dockerfile:

CMD cron && tail -f /var/log/cron.log

It means the only command that will run is cron. So your initial entrypoint for php-fpm service becomes irrelevant. In order to launch several processes in a docker container, you have to use a wrapper script or supervisor. For wrapper script, write it and copy through the build process. The script should have the start commands for all your services.

The CMD will have something like:

CMD ./wrapper_script.sh

For supervisor, there are good tutorials available online that explains how to use supervisor with docker.

Your Dockerfile will look like this:

FROM 'php:7-fpm'
# PDO
RUN docker-php-ext-install pdo_mysql
# Cron
RUN apt-get update -q -q && apt-get install -y cron supervisor
ADD crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN touch /var/log/cron.log
ADD supervisord.conf /etc/supervisor.conf
ENTRYPOINT ["/usr/bin/supervisord -c /etc/supervisor.conf"]

Then create supervisord.conf file:

[supervisord]
nodaemon=true

[program:php-fpm]
command=php-fpm-commands

[program:cron]
command=cron-commands

Replace php-fpm-commands and cron-commands with the commands and options you need to be there for each service.