0

I created a Docker image with Dockerfile for executing a script like get_date.sh on the container but it's not working.

get_date.sh contains:

date
date +"%FORMAT"
var=$(date)
var=`date`
echo "$var">>/var/log/tito_time.log

Dockerfile:

FROM ubuntu:latest
MAINTAINER docker@ekito.fr
RUN apt-get update && apt-get install -y cron && apt-get install nano
COPY get_date.sh  /etc/cron.d/get_date.sh
RUN chmod 0644 /etc/cron.d/get_date.sh
RUN crontab -l | { cat; echo "* * * * * bash /etc/cron.d/get_date.sh"; } | crontab -
CMD cron

The image is created correctly. Running crontab -l in a container shell gives me:

root@92ce260aeb21:/# crontab -l
* * * * *  bash /etc/cron.d/get_date.sh

crontab doesn't execute get_date.sh on the container.

  • How are you starting the container? – David Maze Nov 29 '22 at 01:01
  • I think the problem is with how you add the cronjob. As the `crontab -l` does not list the schedule in your example. If I start a container with latest ubuntu and use your command to add it, and then read it with `crontab -l` I get `* * * * * bash /etc/cron.d/get_date.sh`. Where in your output, the stars (schedule) are missing. Though I don't have a solution for it now. – Dennis van de Hoef - Xiotin Nov 29 '22 at 06:56
  • Does this answer your question? [How to run a cron job inside a docker container?](https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) – Dennis van de Hoef - Xiotin Nov 29 '22 at 07:01
  • @Dennis van de Hoef - Xiotin there are stars i forgot to put when i copy past – Gajenthan Thuraisingam Nov 29 '22 at 08:18
  • Using traditional `cron` implementations under `docker` is tricky. Check out [my answer](https://stackoverflow.com/a/75353647/52499). – x-yuri Feb 05 '23 at 17:33

0 Answers0