[英]Docker, Rails and a cron tasks: how to set up a Docker container to run cron Rails rake tasks
如何正确设置Docker实例以运行执行Rails rake tasks
cronjob tasks
rake tasks
?
我一直在尝试几件事,但遇到了所有这些问题:
cron.d
文件 因为一段时间以来我一直在处理所有这些问题,但是我在互联网上找不到能解决我所有问题的解决方案,所以将我的解决方案粘贴到这里:
〜/ MyApp / crontab:
* * * * * root /bin/bash -l -c 'cd $RAILS_ROOT && bundle exec rake my_task' >> /var/log/app_cron.log 2>&1
〜/ MyApp / Dockerfile:
FROM ruby:2.5.1
# Install dependencies
RUN apt-get update && apt-get -y install cron
# Set an environment variable where the Rails app is installed to inside of Docker image:
ENV RAILS_ROOT /var/www/app
RUN mkdir -p $RAILS_ROOT
# Set our working directory inside the image
WORKDIR $RAILS_ROOT
# Setting env up
ENV RAILS_ENV="production"
ENV RACK_ENV="production"
# Adding gems
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 20 --retry 5 --without development test
# Adding project files
COPY . .
## Cron config
# Add crontab file to the cron.d directory
# Files in /etc/cron.d can not have names with "-" or ".". It can be problematic
COPY crontab /etc/cron.d/app
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/app
# To load the env variables in cron sessions
# without this the user in the cron session won't be able to find commands and Gems
RUN printenv | grep -v "no_proxy" >> /etc/environment
# Run the command on container startup
CMD ["cron", "-f"]
对于更简单的解决方案,您可以使用Arask 。 它在Rails本身中运行定时任务。 无需使用crontab。 它在导轨运行时运行。 只需安装gem,然后将行放在config / initializers / arask.rb中,例如:
arask.create task: 'update:cache', cron: '*/5 * * * *' # Every 5 minutes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.