Run cron service when docker build - php

I have in my docker file :
RUN apt-get -y install cron
RUN cron
COPY symfony.cron /var/spool/cron/crontabs/www-data
RUN chown www-data:crontab /var/spool/cron/crontabs/www-data
CMD ["start.sh"]
And in my start.sh :
echo "+ start cron service"
service cron start
All good, the problem is that crons are not running, and is strange because when I go to the docker container and I do service cron status I get [ ok ] cron is running.
so all good. And when I try to execute one command, is executed well. And when I do crontab -u www-data -l I get the liste with all the crons. So all is good at this point. But the crons are not executed anymore, so I don't understand the problem, maybe some permissions or I don't know. Please help me !!! Thx in advance and sorry for my english.
Any ideas please

You should broadly assume commands like service start don’t work in Docker. Just run the program you’re trying to run directly. Make sure it starts as a foreground process.
CMD ["crond", "-f"]
(If your main container script runs a service start command, and that command exits, and the main container script exits, then the container will exit, which isn’t what you want. If service is backed by systemd or another init system, your container won’t be running that.)

Related

Cron running "docker exec" make my server freeze

I need help to solve a problem with a cron running a docker exec command.
After setting up this cron, my server sometimes gets not responding anymore. No web requests handled, no SSH connection possible. I must restart the server to get it back. It usually happens 3 or 4 times per day.
My cron is setup in my host's crontab :
* * * * * docker exec -w /home/current myphpapp-container bash -c "php artisan schedule:run >> storage/logs/schedule.log"
I'm pretty sure the cron is faulty here because I never had this problem before the cron installation and I don't get it when I disable the cron script.
Docker version is "18.06.3-ce".
The container is a "php:8.0-fpm".
OS is "Debian GNU/Linux 8 (jessie)".
I searched into syslog and others but did not find anything interesting. My cron is minute but I don't even see any progressive load increasing along time. I'm a bit stuck...
Do you have any ideas ? Where should I look to find relevant logs ?
Ok, finally, it looks like a "Docker engine 18.06" related problem. I created a new fresh server with OS and Docker engine up to date. Problem is gone.

PHP-FPM wont start from a Dockerfile

Hello dear community,
I am trying to accomplish something very simple, I want to start a php-fpm service from a docker container using a dockerfile. My dockerfile content is posted here below:
FROM debian
RUN apt-get update && apt-get install php -y && apt-get install php7.3-fpm -y && service php7.3-fpm start
When I build this image from the dockerfile and run it as a container, the php-fpm service is not active.
I even tried it with using docker's "interactive mode" (-i arg) to ensure that the container was not exiting in the case that the service was running as a daemon.
I am confused because the command RUN service php7.3-fpm start from my dockerfile should have started the service.
To successfully start the service inside my container I actually have to manually log into it using the command docker exec -it #containerID bash and run the command service php7.3-fpm start myself, and then the service works and becomes active.
I don't understand why the php-fpm service is not starting automatically from my Dockerfile, any help would be very much appreciated. Thanks in advance!
To a first approximation, commands like service don't work in Docker at all.
A Docker container runs only a single foreground process. That's not usually an init system, or if it is, it's just enough to handle some chores like zombie process cleanup. Conversely, a Docker image only contains a filesystem image and some metadata on how to start that process, but it does not persist any running processes. So for example if you
RUN service php7.3-fpm start
it might record in some file that the service was supposed to have been started, but once the RUN command completes, the running process doesn't exist at all any more.
The easiest way to get a running PHP-FPM setup is to use the Docker Hub php image:
FROM php:7.3-fpm
This should do all of the required setup, including arranging for the FPM server to run as the main container command; you just need to COPY your application code in.
If you really want to run it yourself, you need to make it be the main command of your custom image
CMD ["php-fpm"]
as is done in php:7.3-fpm's Dockerfile.

How to run cron jobs inside php-fpm-alpine docker container?

Hi I don't know how can I run a cron job inside this container.
I've found this: How to run a cron job inside a docker container
But that overrides the CMD, I don't know hot to keep php-fpm working
When you need to run multiple processes in your docker container a solution is to use supervisord as the main instruction. Docker will start and monitor supervisord which in turn will start your other processes.
Docker File Example:
FROM debian:9
...
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/my.conf"]
Supervisord config example (/etc/supervisor/my.conf):
[supervisord]
nodaemon=true
[program:cron]
command=/usr/sbin/crond -f -l 8
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
autorestart=true
[program:php-fpm]
command=docker-php-entrypoint php-fpm
Note that it is desirable to configure supervisord to output the logs to /dev/stdout and /dev/stderr to allow docker to handle these logs. Otherwise you risk your container to slow down over time as the amount of file writes increases.
The main question here is how to make PHP work kind of "in parallel" with the cron. And another answer, besides using supervisor, is to use bash's ability to manage tasks. This is generally mentioned here.
For the Alpine PHP-FPM container and the Cron, the startup script would look like this:
Docker File:
FROM php:8.1-fpm-alpine
RUN apk --update add --no-cache bash
COPY ./crontasks /var/spool/cron/crontabs/root
COPY entrypoint.bash /usr/sbin
RUN chmod a+x /usr/sbin/entrypoint.bash
ENTRYPOINT /usr/sbin/entrypoint.bash
entrypoint.bash file (the magic is here)
#!/bin/bash
# turn on bash's job control
set -m
# Start the "main" PHP process and put it in the background
php-fpm &
# Start the helper crond process
crond
# now we bring the primary process back into the foreground
fg %1
It is important to keep in mind that the cron job syntax in Alpine is different from Debian. And different folders are used for tasks.

Cron job is not running

I've tried just about everything to get my cron script to work but am not having any luck. I've been through many mini tutorial several times too! Here's what I'm trying to run...
/usr/bin/php -q /home/username/public_html/yourfilename.php
On cpanel even i am not getting email after running time of my crone job.
thank you
Try this
/usr/bin/php -q /home/username/public_html/yourfilename.php >/dev/null
You need to check below points.
The file location is correct.
check that the crond services running or not. you can check with below commend :
/etc/init.d/crond status
and you should get status running with pid. if you can got it then move on point 3 otherwise. try to restart the cron service via below command:
/etc/init.d/crond restart
and check the logs via below command :
tail -f /var/log/cron
if you getting the cron service working then check your lfd service working on not.
/etc/init.d/lfd status
if its running then check your email proper configure on lfd.
if lfd fine then run below command as root to add cron
crontab -e
give your cron at the end of file with correct format.
sure that time it will work.
if getting any issue in any point. let me know here.

PhP Exec GIT command from crontab

I have a deploy script which i have to execute every time i change my config files. Which is a bummer.
I tried to add the deploy script to my crontab, but from the logs i see that this command is failing in crontab because you are tehnically not in a git repo
exec('git log -1 --format="%H"')
This fella should deliver a revision number so the code can use it, but it dous not execute when you run the script from crontab.
Anyone has any idea how to make it work?

Categories