rails,docker - cron任务不运行

Cit*_*sed 4 cron ruby-on-rails whenever docker

我的crontasks来自docker schedule.rb容器不起作用,但 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
Run Code Online (Sandbox Code Playgroud)

我可以在容器中手动运行此命令,它可以工作.似乎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"]
Run Code Online (Sandbox Code Playgroud)

jma*_*nry 7

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

如果你想,当你开始你的容器启动cron的,你应该运行cronCMD.我删除RUN service cron start并更改了你的Dockerfile 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
Run Code Online (Sandbox Code Playgroud)

减少图像层数的最佳做法是,例如,您应该始终在同一个RUN语句中将RUN 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
Run Code Online (Sandbox Code Playgroud)