After several days of messing around with Docker, I have not found a solution to my problem.
In short, I have tried to add cron to php:8.1-apache-bullseye and pass it a file with the task that I want the cron service to execute:
cron-task content:
* * * * * /var/www/html/Asset/resource/cron/cron.sh >> /var/log/cron/cron.log 2>&1
content of cron.sh:
#!/bin/bash
Get current time
current_time=$(date +"%H:%M:%S")
Warning message
message="warning: Hello, world!!"
Print time and message to stdout
echo "${current_time}: ${message}"
To set up everything I have a section of my docker-compose.yml where I set up a webserver service as follows:
webserver:
env_file:
- .env
container_name: ${LH_SYSTEM_NAME}-Web-Server
build:
context: ./bin/${LH_PHP_ENVIRONMENT}
restart: always
networks:
- lamp-network
depends_on:
- database
volumes:
- ${LH_PROJECT_ROOT}:/var/www/html:rw
- ${LH_PROJECT_ROOT}${LH_DOCUMENT_ROOT-./public}:/var/www/html/public:rw
- ${LH_VHOSTS_DIR}:/etc/apache2/sites-enabled
- ${LH_PHP_INI}:/usr/local/etc/php/php.ini
- ${LH_CRON}:/etc/script/cron-task
- ${LH_LOG_CRON}:/var/log/cron
- ${LH_LOG_DIR}:/var/log/apache2
environment:
LH_WEB_MASTER: ${LH_WEB_MASTER}
VIRTUAL_HOST: ${LH_WEB_SERVER_DOMAIN}
LH_APACHE_DOCUMENT_ROOT: ${LH_APACHE_DOCUMENT_ROOT}
LH_DOCUMENT_ROOT: ${LH_DOCUMENT_ROOT}
HOST_MACHINE_MYSQL_PORT: ${LH_HOST_MACHINE_MYSQL_PORT}
MYSQL_DATABASE: ${LH_MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: ${LH_MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${LH_MYSQL_USER}
MYSQL_PASSWORD: ${LH_MYSQL_PASSWORD}
extra_hosts:
- "host.docker.internal:host-gateway"
command: [ "/bin/sh", "-c", "chmod +x /var/www/html/Asset/resource/cron/cron.sh && cp /etc/script/cron-task /etc/cron.d/cron-task && chmod 0644 /etc/cron.d/cron-task && crontab /etc/cron.d/cron-task && cron -f && service cron restart" ]
Note: We can ignore the Environment Variables since they were all tested and verified one by one and do what they are expected to do...
Additionally, I am implementing the command section to see if this solves the problems, but from what I have seen and tested, it doesn't work either.
Commands that are executed in command:
chmod +x /var/www/html/Asset/resource/cron/cron.sh
cp /etc/script/cron-task /etc/cron.d/cron-task
chmod 0644 /etc/cron.d/cron-task
crontab /etc/cron.d/cron-task
cron -f
service cron restart
so that the file inside the container is located correctly... all this works well, even the commands executed in command I have not had any interruption or error messages in the construction and start-up of the containers.
So my Dockerfile contains the following:
FROM php:8.1-apache-bullseye
ARG DEBIAN_FRONTEND=noninteractive
RUN a2enmod rewrite headers
RUN service apache2 restart
RUN apt-get update &&
apt-get upgrade -y --no-install-recommends --fix-missing
RUN apt-get install -y cron nano wget dialog build-essential git curl zip openssl --no-install-recommends --fix-missing
RUN apt-get -y autoremove &&
apt-get clean
ENV VISUAL=nano
ENV EDITOR=nano
RUN service cron start
RUN rm -rf /usr/src/* &&
rm -rf /var/lib/apt/lists/*
RUN update-rc.d cron enable
This doesn't give me any problems either... now I go to the terminal of my webserver container, and even though I then validate with:
service cron status
and the output says that it is running...
Viewing the contents of crontab -e:
In the end the problem is that nothing ever happens automatically... as if the cron service were stopped...
Just in case I ran tests on the .sh file and everything works fine from the terminal.
even add the record to the log... but when I wait for the cron service to do it, nothing happens... and I no longer know what to do or where to look...
Update:
the output of: ps aux | grep cron
was:
root 1 0.0 0.0 2480 516 ? Ss 21:46 0:00 /bin/sh -c chmod +x /var/www/html/Asset/resource/cron/cron.sh && cp /etc/script/cron-task /etc/cron.d/cron -task && chmod 0644 /etc/cron.d/cron-task && crontab /etc/cron.d/cron-task && cron -f && service cron restart
root 11 0.0 0.0 3744 2444 ? S 21:46 0:00 cron -f
root 78 0.0 0.0 3240 712 pts/0 S+ 22:11 0:00 grep cron
So it seems that despite everything I've tried, the task is not in the cron... I still don't know what to do...
