rails,docker - cron任务不运行

时间:2017-04-22 12:31:16

标签: ruby-on-rails docker cron whenever

来自schedule.rb的我的crontasks在docker容器上不起作用,但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"]

1 个答案:

答案 0 :(得分:6)

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

如果您想在启动容器时启动cron,则应在cron中运行CMD。我删除了RUN service cron start并更改了ENTRYPOINT

,从而修改了您的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
CMD cron && bundle exec puma

减少图像层数的最佳做法是,例如,您应该始终在同一个RUN语句中将RUN apt-get update与apt-get install结合使用,并在以下后清除apt文件:{ {1}}

rm -rf /var/lib/apt/lists/*