-1

I have a dockerfile image based on ubuntu. Iam trying to make a bash script run each day but the cron never runs. When the container is running, i check if cron is running and it is. the bash script works perfectly and the crontab command is well copied inside the container. i can't seem to find where the problem is coming from.

Here is the Dockerfile:

FROM snipe/snipe-it:latest

ENV TZ=America/Toronto

RUN apt-get update \
    && apt-get install awscli -y \
    && apt-get clean \
    && apt-get install cron -y \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir /var/www/html/backups_scripts /var/www/html/config/scripts

COPY config/crontab.txt /var/www/html/backups_scripts
RUN /usr/bin/crontab /var/www/html/backups_scripts/crontab.txt

COPY config/scripts/backups.sh /var/www/html/config/scripts

CMD ["cron","-f"]

The last command CMD doesn't work. And as soon as i remove the cmd command i get this message when i check the cron task inside the container:

root@fcfb6052274a:/var/www/html# /etc/init.d/cron status
 * cron is not running

Even if i start the cron process before the crontab, the crontab is still not launched

This dockerfile is called by a docker swarm file (compose file type). Maybe the cron must be activated with the compose file.

How can i tackle this problem ??? Thank you

YoussHark
  • 558
  • 1
  • 9
  • 26

2 Answers2

0

You need to approach this differently, as you have to remember that container images and containers are not virtual machines. They're a single process that starts and is maintained through its lifecycle. As such, background processes (like cron) don't exist in a container.

What I've seen most people do is have the container just execute whatever you're looking for it to do on a job like do_the_thing.sh and then using the docker run command on on the host machine to call it via cron.

So for sake of argument, let's say you had an image called myrepo/task with a default entrypoint of do_the_thing.sh

On the host, you could add an entry to crontab:

# m h dom mon dow user  command
0 */2    * * *   root    docker run --rm myrepo/task

Then it's down to a question of design. If the task needs files, you can pass them down via volume. If it needs to put something somewhere when it's done, maybe look at blob storage.

Dockstar
  • 1,005
  • 11
  • 15
0

I think this question is a duplicate, with a detailed response with lots of upvotes here. I followed the top-most dockerfile example without issues.

Your CMD running cron in the foreground isn't the problem. I ran a quick version of your docker file and exec'ing into the container I could confirm cron was running. Recommend checking how your cron entries in the crontab file are re-directing their output.

Expanding on one of the other answers here a container is actually a lot like a virtual machine, and often they do run many processes concurrently. If you happen to have any other containers running you might be able to see this most easily by running docker stats and looking at the PID column.

Also, easy to examine interactively yourself like this:

$ # Create a simple ubuntu running container named my-ubuntu
$ docker run -it -h my-ubuntu ubuntu
root@my-ubuntu$ ps aw # Shows bash and ps processes running.
root@my-ubuntu$ # Launch a ten minute sleep in the background.
root@my-ubuntu$ sleep 600 &
root@my-ubuntu$ ps aw # Now shows sleep also running.
fifofonix
  • 311
  • 2
  • 10