I would like to run a bash script periodically inside a Docker container (my work is based on this answer: https://stackoverflow.com/a/37458519/6240756)
Here is my script hello.sh
:
#!/bin/sh
echo "Hello world" >> /var/log/cron.log 2>&1
Here is the cron file hello-cron
:
# m h dom mon dow command
* * * * * /app/hello.sh
# Empty line
And here is my Dockerfile:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y cron
# Add backup script
COPY hello.sh /app/
RUN chmod +x /app/hello.sh
# Configure the cron
# Copy file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Apply cron job
RUN crontab /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Start the cron
CMD cron && tail -f /var/log/cron.log
I build and run the container but nothing happens. I should see "Hello world" displayed every minutes.
If I replace the call to the script in the cron file by directly echo "Hello world" >> /var/log/cron.log 2>&1
it works, I see "Hello world" every minutes
What am I doing wrong?
EDIT
And the Docker commands:
docker build -t hello-cron .
docker run -t -i hello-cron