将Dockerfile配置为使用Whenver和Rake运行Cron任务



我想使用Docker创建一个容器,它将负责根据Wheney-gem的配置启动重复的rake任务。我有一个简单的ruby项目(没有rails/sinatra(,结构如下:

Gemfile

source 'https://rubygems.org'
gem 'rake', '~> 12.3', '>= 12.3.1'
gem 'whenever', '~> 0.9.7', require: false
group :development, :test do
gem 'byebug', '~> 10.0', '>= 10.0.2'
end
group :test do
gem 'rspec', '~> 3.5'
end

config/schedule.rb:(无论何时配置(

ENV.each { |k, v| env(k, v) }
every 1.minutes do
rake 'hello:start'
end

lib/tasks/hello.rb:(rake配置(

namespace :hello do
desc 'This is a sample'
task :start do
puts 'start something!'
end
end

Dockerfile

FROM ruby:2.5.3-alpine3.8
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && 
apk update && apk upgrade && 
apk add build-base bash dcron && 
apk upgrade --available && 
rm -rf /var/cache/apk/* && 
mkdir /usr/app
WORKDIR /usr/app
COPY Gemfile* /usr/app/
RUN bundle install
COPY . /usr/app
RUN bundle exec whenever --update-crontab
CMD ['sh', '-c', 'crond && gulp']

我已经使用了以下资源来获得

  • 如何在docker容器中运行cron作业
  • https://github.com/renskiy/cron-docker-image/blob/master/alpine/Dockerfile
  • https://stackoverflow.com/a/43622984/5171758<-非常接近我想要但没有成功

如果我使用命令行调用我的rake任务,我会得到我想要的结果。

$ rake 'hello:start'
start something!

然而,我不知道如何使用Docker使其工作。容器已经构建,但没有写入日志,没有显示输出,什么也没发生。有人能帮我展示一下我做错了什么吗?

构建命令

docker build -t gsc:0.0.1 .
docker container run -a stdin -a stdout -i --net host -t gsc:0.0.1 /bin/bash

谢谢大家。干杯

这是我上面列出的问题的解决方案。我在Dockerfileschedule.rb遇到了一些问题。这是我必须改变的,以使其正确工作。

Dockerfile

  • 错误的echo调用
  • 错误的bundle命令
  • 更改ENTRYPOINT而不是CMD
FROM ruby:2.5.3-alpine3.8
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/main && 
apk update && apk upgrade && 
apk add build-base bash dcron && 
apk upgrade --available && 
rm -rf /var/cache/apk/* && 
mkdir /usr/app
WORKDIR /usr/app
COPY Gemfile* /usr/app/
RUN bundle install
COPY . /usr/app
RUN bundle exec whenever -c && bundle exec whenever --update-crontab && touch ./log/cron.log
ENTRYPOINT crond && tail -f ./log/cron.log

config/schedule.rb

  • 无需ENV.each
every 1.minutes do
rake 'hello:start'
end

更新

我创建了一个GitHub存储库和一个DockerHub存储库来与社区分享这一进展。

最新更新