0

I followed the suggested solution for this question. And came up with the following docker file

FROM ubuntu:16.04
ADD write_time.py /
USER root
RUN apt-get update && \
    apt-get install -y python cron && \
    chmod +x /write_time.py && \
    (crontab -l 2>/dev/null; echo "* * * * * cd / && /usr/bin/python /write_time.py >> test.out") | crontab -

the write_time.py is

#!/usr/bin/env python

import datetime

time = datetime.datetime.now()
time = time.strftime("%Y-%m-%dT%H:%M:%S.%f")
print(time)

with open("time.txt", "a") as f:
    f.write(time+"\n")

After i build with the below command and run it -

docker build . -t se
docker run -it se

I exec into the contaier, to check if either test.out or test.txt is created at / but i do not see either.(have waited for more than 2 mins)

Anything i am doing wrong here?

Thanks!

pa1
  • 778
  • 3
  • 11
  • 26
  • Possible duplicate of [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) – LinPy Oct 17 '19 at 09:28
  • 1
    Absent any `CMD`, the `docker run` command will run the default from the `ubuntu` base image, an interactive shell. That shell is the _only_ thing the container runs – there are no other daemons of any kind, including a cron daemon. – David Maze Oct 17 '19 at 10:41
  • I will recomend to use alpine base image from cron – Adiii Oct 17 '19 at 10:47

1 Answers1

0

Solved it.

Docker CMD needs to be set to cron deamon.

pa1
  • 778
  • 3
  • 11
  • 26