【问题标题】:rails, whenever and docker - cron tasks doesn't runrails,无论何时和 docker - cron 任务不运行
【发布时间】:2017-09-19 10:21:36
【问题描述】:

我来自 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"]

【问题讨论】:

    标签: ruby-on-rails docker cron whenever


    【解决方案1】:

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

    如果要在启动容器时启动 cron,则应在 CMD 中运行 cron。我通过删除 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 apt-get update 与 apt-get install 结合在同一个 RUN 语句中,并在之后清理 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
    

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 2018-05-11
      • 2014-11-30
      • 1970-01-01
      • 2019-10-02
      • 2011-03-04
      • 2014-05-27
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多