I would like to give an example of how I created a cron job so that you can implement your own. To run cron container first create the following files:-
crontab.txt
entry.sh
script.sh
Dockerfile
See the following what should be in the files as mentioned above.
crontab.txt
*/30 * * * * /script.sh >> /var/log/script.log
Entry.sh
#!/bin/sh
# start cron
/usr/sbin/crond -f -l 8
script.sh
#!/bin/sh
# code goes here.
echo "This is a script, run by cron!"
Dockerfile
FROM alpine:3.3
ADD crontab.txt /crontab.txt
ADD script.sh /script.sh
COPY entry.sh /entry.sh
RUN chmod 755 /script.sh /entry.sh
RUN /usr/bin/crontab /crontab.txt
CMD ["/entry.sh"]
Then, build the docker file using the following commands
Sudo docker build <dockerfile-name> .
Sudo docker run -d <dockerfile-name>
Thus, we can build a cron job inside a Docker container. So now you can easily implement by following through this.
If you are searching for more information on Docker then you can join Docker Training by Intellipaat.