铁轨,每当和docker – cron任务不运行

schedule.rb我crontasksdocker容器上不工作,但crontab -l结果已经包含这些行:

 # Begin Whenever generated tasks for: /app/config/schedule.rb 45 19 * * * /bin/bash -l -c 'bundle exec rake stats:cleanup' 45 19 * * * /bin/bash -l -c 'bundle exec rake stats:count' 0 5 * * * /bin/bash -l -c 'bundle exec rake stats:history' # End Whenever generated tasks for: /app/config/schedule.rb 

我可以在容器中手动运行这个命令,它的工作原理。 好像cron不启动。

Dockerfile:

 FROM ruby:2.4.0-slim RUN apt-get update RUN apt-get install -qq -y --no-install-recommends build-essential libpq-dev cron postgresql-client RUN cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime ENV LANG C.UTF-8 ENV RAILS_ENV production ENV INSTALL_PATH /app RUN mkdir $INSTALL_PATH RUN touch /log/cron.log ADD Gemfile Gemfile.lock ./ WORKDIR $INSTALL_PATH RUN bundle install --binstubs --without development test COPY . . RUN bundle exec whenever --update-crontab RUN service cron start ENTRYPOINT ["bundle", "exec", "puma"] 

在Dockerfile中,RUN命令仅在构build映像时执行。

如果您想在启动容器时启动cron,则应该在CMD运行cron 。 我通过删除RUN service cron start和更改您的ENTRYPOINT修改您的ENTRYPOINT

 FROM ruby:2.4.0-slim RUN apt-get update RUN apt-get install -qq -y --no-install-recommends build-essential libpq-dev cron postgresql-client RUN cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime ENV LANG C.UTF-8 ENV RAILS_ENV production ENV INSTALL_PATH /app RUN mkdir $INSTALL_PATH RUN touch /log/cron.log ADD Gemfile Gemfile.lock ./ WORKDIR $INSTALL_PATH RUN bundle install --binstubs --without development test COPY . . RUN bundle exec whenever --update-crontab CMD cron && bundle exec puma 

减less图像的层数是一个最佳实践,例如,您应该总是在同一个RUN语句中将apt apt-get update和apt-get install结合起来,然后清理apt文件: rm -rf /var/lib/apt/lists/*

 FROM ruby:2.4.0-slim RUN apt-get update && \ apt-get install -qq -y --no-install-recommends build-essential libpq-dev cron postgresql-client \ rm -rf /var/lib/apt/lists/* && \ cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime ENV LANG C.UTF-8 ENV RAILS_ENV production ENV INSTALL_PATH /app RUN mkdir $INSTALL_PATH && \ touch /log/cron.log ADD Gemfile Gemfile.lock ./ WORKDIR $INSTALL_PATH RUN bundle install --binstubs --without development test COPY . . RUN bundle exec whenever --update-crontab CMD cron && bundle exec puma