-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

How can i tackle this problem ??? Thank you

YoussHark
  • 558
  • 1
  • 9
  • 26
  • Docker images usually don't have any services running, though this also depends on the specific image. Have you checked whether it's even supposed to be there? – tripleee Oct 27 '19 at 20:33
  • @tripleee it is an ubuntu based image + i do install cron inside the dockerfile. And on top of that even when i exec the container, start the cron process manually, it still never run the crontab. – YoussHark Oct 27 '19 at 20:38
  • Your `Dockerfile` doesn't demonstrate this; could you please [edit] your question to clarify where and how this happens? – tripleee Oct 27 '19 at 20:47
  • @tripleee snipe/snipe-it:latest is an ubuntu based image and the cron is installed with " && apt-get install cron -y \ ". the crontab is copied but never runs – YoussHark Oct 27 '19 at 20:52
  • @tripleee. I Had to make another service to make it work alongside the original service. And on top of that i found the env vars were not injected during the processing of the bash script, why ? Because a sh script which runs due to a cron, we actually need to source+exports the vars. A cronjob opens a new shell that is the reason why. – YoussHark Oct 29 '19 at 18:11

1 Answers1

2

You are running crontab in a RUN statement. But that only runs during image creation, not when you actually use the resulting image.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • this image is used by a docker stack, so how can i tackle this issue within the dockerfile or the compose file ? thanks – YoussHark Oct 27 '19 at 20:56
  • 1
    The *default* startup command in `CMD` should probably be a script which starts up any and all services and takes care to keep them running. Maybe look for ways to run a service manager in Docker so you don't have to reinvent clumsy hacks with endless loops. But realize that conventional service managers like `runit` are often a bad fit for Docker. – tripleee Oct 28 '19 at 04:59