How do I run a cron job inside a docker container

+14 votes
I am trying to run a cron job inside a docker container but it’s not working out for me.

My container has only cron. daily and cron.weekly file

crontab, cron.d, cron.hourly ... are not present in my container

I have tried to use crontab -e  but it’s also not working

The container which I am using runs with /bin/bash
Apr 7, 2018 in DevOps & Agile by Atul
• 10,240 points

edited Aug 27, 2018 by Kalgi 16,488 views

5 answers to this question.

+1 vote
Best answer
If you can SSH into your server and it’s running a distro of Linux that has cron available then you’re good to go. If you spin up a server on any other major cloud provider and install Ubuntu you’re all set. Cron will be available to you on the Docker host. Done deal. Then you can just run whatever task you need.
answered Jul 27, 2018 by Kalgi
• 52,350 points

selected May 7, 2019 by Vardhan
+1 vote

Here is how I run one of my cron containers. Docker file:

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

*/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!"

Execute this command to build

docker build -t mycron .

Execute this command to run

docker run -d mycron

Just add your own scripts and edit the crontab.txt and just build the image and run.I hope it will resolve all your query.

answered Apr 7, 2018 by shubham
• 7,340 points

edited Jul 27, 2018 by Vardhan
0 votes
Running a cron job from inside a container is not recommended. A better option is to run it from the host itself.
answered May 7, 2019 by Laksha
0 votes

In the same folder as your Dockerfile, create a file called crontab:

* * * * * /file.sh
# Mandatory blank line

Then add the following to your Dockerfile:

COPY crontab /etc/cron.d/work
RUN chmod 0644 /etc/cron.d/work
RUN service cron start

Rebuild the Docker image, and you’re all set!

answered May 7, 2019 by Vinod
0 votes

Try something like this:

FROM ubuntu:latest

# Install cron
RUN apt-get -y install cron

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

# Setup cron job
RUN (crontab -l ; echo "* * * * * echo "Hello world" >> /var/log/cron.log") | crontab

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
answered May 7, 2019 by Yesha

Related Questions In DevOps & Agile

+1 vote
2 answers

How do I run Apache server from Docker without mapping to a location?

If you're seeing a 500 error, that's ...READ MORE

answered Oct 18, 2018 in DevOps & Agile by lina
• 8,220 points
2,164 views
0 votes
1 answer

How do I assign a port mapping to an existing Docker container?

Below are some steps which you can ...READ MORE

answered Sep 19, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
9,289 views
0 votes
1 answer

How do I restart a single container with docker-compose?

Actually it's not that difficult. Here is ...READ MORE

answered Nov 8, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
7,453 views
0 votes
1 answer
+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,100 points
2,687 views
+3 votes
2 answers

How do I set max lifetime for Docker Container?

There is one more way which you ...READ MORE

answered Oct 18, 2018 in DevOps & Agile by Kalgi
• 52,350 points
3,587 views
0 votes
2 answers

Running a cron job in a docker conratiner

Let’s create a new file called "crontab" ...READ MORE

answered Aug 7, 2018 in DevOps & Agile by Kalgi
• 52,350 points
12,480 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