Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

I tried to run a cron job inside a Docker container

but nothing works for me

my container have only cron.daily and cron.weekly file

crontab,cron.d,cron.hourly ... are absent in my container

crontab -e also not working

my container runs with /bin/bash

1 Answer

0 votes
by (50.2k points)

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.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...