在docker容器中执行定时任务

Dockerfile

FROM ubuntu:latest

# Install Pip
RUN apt-get update
RUN apt-get install -y cron

# Copy shell
ADD ./echo.sh /

RUN chmod +x /echo.sh

# Add crontab file in the cron directory
ADD cronfile /etc/cron.d/simple-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/simple-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

cron file

* * * * * root /echo.sh

echo.sh

echo "$(date): echo tag" >> /var/log/cron.log 2>&1