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 6, 2018 in DevOps & Agile by Atul
• 10,240 points

edited Aug 27, 2018 by Kalgi 3,509 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
• 46,110 points

selected May 7 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
RUN chmod 755 / /
RUN /usr/bin/crontab /crontab.txt

CMD ["/"]

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


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

# 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 6, 2018 by shubham
• 6,890 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 by Laksha
0 votes

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

* * * * * /
# 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 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 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,160 points
0 votes
1 answer
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,510 points
0 votes
1 answer
0 votes
1 answer
0 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
• 57,300 points
+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
• 46,110 points
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
• 46,110 points