I also came across the same question when using docker with php-fpm
and nginx
. The way I solved the problem was similar to the answer given previously.
TL;DR
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 && docker-php-entrypoint php-fpm
Adding last line to start the process that php-fpm
intended to run
As stated above,
In docker container, you can only have one entrypoint or a command executed when you start the container.
Problem
But when you add our own entrypoint to the docker container by doing CMD ["custom_script.sh"]
or ENTRYPOINT ["custom_script.sh"]
, the default entrypoint script gets replaced by your custom one (in our case custom_script.sh
).
The way I came across solution to the problem was by writing script in such a way that doesn't replace original instead extend.
Solution
- To learn which script the docker container runs by default was found using
docker-compose ps
after running the container without the entrypoint script. The result in my machine looks like below;
Name Command State Ports
----------------------------------------------------------------------------------------------------------------
appointmentlaravel_appointment_app_1 docker-php-entrypoint php-fpm Up 0.0.0.0:9007->6001/tcp, 9000/tcp
As you can see, by default, the script container runs is docker-php-entrypoint php-fpm
so.
- We can create custom script in such a way that at the end runs the same script that it ran originally as shown below;
# arbitary code to run when docker-container starts
docker-php-entrypoint php-fpm
In your case, the dockerfile
may look like;
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 && docker-php-entrypoint php-fpm
Look how I replaced the last line of your dockerfile to run docker-php-entrypoint php-fpm
.