2

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:

enter image description here

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...

1 Answers1

0

After trying different ways of manipulating the files and creating the needs in an appropriate order to integrate the cron utility into a container and keep it working, this was necessary:

  1. Remove the command and some volumenes param/section from my docker-compose.yml file, Now it looks like this:
version: "3.8"

services: reverse-proxy: env_file: - .env container_name: Proxy-Server image: jwilder/nginx-proxy volumes: - /var/run/docker.sock:/tmp/docker.sock:ro ports: - "${LH_HOST_MACHINE_UNSECURE_HOST_PORT}:80" depends_on: - webserver - phpmyadmin networks: - lamp-network extra_hosts: - "${LH_WEB_SERVER_DOMAIN}:127.0.0.1" - "${LH_PHPMYADMIN_DOMAIN}:127.0.0.1" - "${LH_CRON_DOMAIN}:127.0.0.1" - "${LH_INCRON_DOMAIN}:127.0.0.1" 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}:/var/www/html/public:rw - ${LH_VHOSTS_DIR}:/etc/apache2/sites-enabled - ${LH_PHP_INI}:/usr/local/etc/php/php.ini - ${LH_LOG_DIR}:/var/log/apache2 - ${LH_LOG_CRON}:/var/log/cron 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" database: env_file: - .env build: context: ./bin/${LH_DATABASE} container_name: ${LH_SYSTEM_NAME}-${LH_DATABASE} restart: always networks: - lamp-network ports: - "127.0.0.1:${LH_HOST_MACHINE_MYSQL_PORT}:${LH_HOST_MACHINE_MYSQL_PORT}" volumes: - ${LH_MYSQL_INITDB_DIR}:/docker-entrypoint-initdb.d - ${LH_MYSQL_DATA_DIR}:/var/lib/mysql - ${LH_MYSQL_LOG_DIR}:/var/log/mysql environment: MYSQL_ROOT_PASSWORD: ${LH_MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${LH_MYSQL_DATABASE} MYSQL_USER: ${LH_MYSQL_USER} MYSQL_PASSWORD: ${LH_MYSQL_PASSWORD} phpmyadmin: env_file: - .env container_name: ${LH_SYSTEM_NAME}-phpmyadmin image: phpmyadmin/phpmyadmin restart: always depends_on: - database environment: VIRTUAL_HOST: ${LH_PHPMYADMIN_DOMAIN} PMA_HOST: database PMA_PORT: 3306 PMA_USER: root PMA_PASSWORD: ${LH_MYSQL_ROOT_PASSWORD} MYSQL_ROOT_PASSWORD: ${LH_MYSQL_ROOT_PASSWORD} MYSQL_USER: ${LH_MYSQL_USER} MYSQL_PASSWORD: ${LH_MYSQL_PASSWORD} UPLOAD_LIMIT: ${LH_UPLOAD_LIMIT} MEMORY_LIMIT: ${LH_MEMORY_LIMIT} volumes: - /sessions - ${LH_PHP_INI}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini networks: - lamp-network networks: lamp-network: driver: bridge

  1. I have changed the scripting in my Dockerfile with another approach:
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 --no-install-recommends --fix-missing build-essential dialog nano apt-utils cron wget git curl zip openssl

RUN apt-get -y autoremove &&
apt-get clean

RUN rm -rf /usr/src/* &&
rm -rf /var/lib/apt/lists/*

RUN mkdir -p /var/log/cron &&
chmod 755 /var/log/cron &&
touch /var/log/cron/cron.log

RUN echo "* * * * * root /var/www/html/Asset/resource/cron/cron.sh >> /var/log/cron/cron.log 2>&1" > /etc/cron.d/cron-task &&
chmod 0644 /etc/cron.d/cron-task

CMD cron && /usr/local/bin/apache2-foreground && chmod +x /var/www/html/Asset/resource/cron/cron.sh && tail -f /var/log/cron/cron.log

The most important thing was the implementation of CMD to solve almost all the problems detected and create the files from Dockerfile instead of mounting them.

I'm not sure if it's the right way or the best way to do it, but it's really been the only way to get it going.

additional notes:

  • It is important to incorporate the entry point or that implements the image at the beginning of the CMD, since using the CMD replaces the default CMD causing problems.
  • It is also important that if tail... is implemented, it is done at the end since this usually blocks the script and execution.