Wednesday, February 7, 2024

[SOLVED] "cron is not running" After runing a cron job from a Dockerfile

Issue

I'm trying to do a hourly backup of a mysql database that is on a docker with a cronjob but when i build the docker and do service cron status it always says "cron is not running".

It ONLY works when i execute the command service cron start inside the docker but i cannot work with that.

My Dockerfile.mysql:

FROM ubuntu/mysql

COPY ./app/db/mydbname.sql /docker-entrypoint-initdb.d/

# Add sql_backup file in the cron directory
ADD sql_backup /etc/cron.d/sql_backup

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD crond -l 2 -f

sql_backup

* * * * * mysqldump -uroot -pmypassword mydb > /backups/mydbname.sql
* * * * * echo "Hello world" >> /var/log/cron.log 2>&1

docker-compose.yaml:

    db:
    image: ubuntu/mysql
    build:
      context: .
      dockerfile: ./Dockerfile.mysql
    # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
    # (this is just an example, not intended to be a production configuration)
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    network_mode: bridge

I have already tried to switch the last line of the DockerFile.mysql to CMD ["cron", "-f"] CMD cron && tail -f /var/log/cron.log etc.


Solution

I Couldn´t have Cron and MySQL running on the same container. I have moved cron to another container and i have now the MySQL backups running.



Answered By - Henrique João
Answer Checked By - David Goodson (WPSolving Volunteer)