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

Configure Dockerfile to Run Cron Tasks using Whenver and Rake

我想使用 Docker 创建一个容器,它将负责根据 Whenever 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

Docker文件:

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']

我使用了以下资源来达到这一点

如果我使用命令行调用我的 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

  • 回声调用错误
  • 错误的捆绑命令
  • 更改 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 repository and a Docker Hub repository 来与社区分享这一进展。