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:
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:
For supervisor, there are good tutorials available online that explains how to use supervisor with docker.
Your Dockerfile will look like this:
Then create supervisord.conf file:
Replace php-fpm-commands and cron-commands with the commands and options you need to be there for each service.