Running a cron job in a docker conratiner

0 votes
I am trying to execute a cron job in a docker container. So, I checked my container and seems crontab, cron.d, cron.hourly files are absent from my container. cron.daily and cron.weekly are present. I'm running the container using /bin/bash.
Apr 13, 2018 in DevOps & Agile by ffdfd
• 5,550 points
11,896 views
How is running cron job on a host different from running it on a container?

Running a cron job on a host is the most preferable method. It's very easy to set up. All you have to do is ssh into your host and if its Linux distro then cron is anyways available.

Only when the above is not possible it is advised to bring up a container with cron

2 answers to this question.

0 votes

This is the way I ran my cron containers.

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"]

crontab.txt file:

*/30 * * * * /script.sh >> /var/log/script.log

entry.sh file:

#!/bin/sh

# start cron
/usr/sbin/crond -f -l 8

script.sh file:

#!/bin/sh

# code goes here.
echo "This is a script, run by cron!"

to buiild:

docker build -t mycron .

and finally to run:

docker run -d mycron

add the scripts you want, edit the crontab.txt and build the image. It should run now.

answered Apr 13, 2018 by ajs3033
• 7,300 points
0 votes

Let’s create a new file called "crontab" to describe our job.

The following DockerFile describes all the steps to build your image

FROM ubuntu:latest
MAINTAINER docker@ekito.fr
RUN apt-get update && apt-get -y install cron
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Apply cron job
RUN crontab /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

Build and run it:

sudo docker build --rm -t ekito/cron-example .
sudo docker run -t -i ekito/cron-example
answered Aug 7, 2018 by Kalgi
• 52,360 points

Related Questions In DevOps & Agile

+14 votes
5 answers

How do I run a cron job inside a docker container

If you can SSH into your server ...READ MORE

answered Jul 27, 2018 in DevOps & Agile by Kalgi
• 52,360 points
15,907 views
0 votes
1 answer

How to a run deployed app in Docker for Windows?

To deploy an ASP.NET web application to ...READ MORE

answered Aug 30, 2018 in DevOps & Agile by Tyrion anex
• 8,700 points
897 views
0 votes
1 answer
+2 votes
1 answer

What is the difference between a container and an image in Docker?

 Below is reference which you can follow to know ...READ MORE

answered Sep 20, 2018 in DevOps & Agile by shubham
• 7,340 points
911 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,090 points
2,467 views
0 votes
1 answer

Can I run a program on release failure in TFS?

You can try and do the following ...READ MORE

answered Apr 26, 2018 in DevOps & Agile by ajs3033
• 7,300 points
520 views
+5 votes
3 answers

Steps to Call Python method in BuildBot

To run python code, you must write ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by ajs3033
• 7,300 points

edited Oct 12, 2018 by Kalgi 1,376 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP