2017-04-22 77 views
2

我從schedule.rb crontasks不上搬運工容器中工作,但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"] 

回答

3

在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的與更新的apt-get安裝在相同的運行語句,乾淨易文件後: 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