Add cron job to a docker container


Recommended Posts

Thanks for the tip.

I managed to add cron to needo's image like this:

 

Dockerfile:

FROM needo/mariadb:latest
MAINTAINER soana <[email protected]>
ENV DEBIAN_FRONTEND noninteractive

# Add our crontab file
ADD crontab.txt /config/crontab.txt
ADD cronjob /config/cronjob

#Use the crontab file
RUN crontab /config/crontab.txt

# Start cron
RUN cron

VOLUME /backup

 

cronjob file:

#!/bin/bash
##backup databases
mysqldump -u root MyMusic46 > /backup/MyMusic_$(date +%Y-%m-%d).sql
mysqldump -u root MyVideos78 > /backup/MyVideos_$(date +%Y-%m-%d).sql

##Delete files "My*" older than 5 days
find /backup/My* -mtime +4 -exec rm {} \;

Reminder don't forget to chmod +x this file before building the image

 

crontab.txt

00 3 * * * /config/cronjob

 

 

Now I'm looking into a way to be able to edit the crontab.txt and cronjob files while the container is running eliminating the need to rebuild the image every time one of these files have to change.

I'm sure someone will come up with a much more elegant solution eventually but until then I have my sql backups and I'm happy with my Linux noob solution.

 

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.