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

问题描述 投票:0回答:1

我想使用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

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

我已经使用以下资源来实现这一点

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

谢谢大家。干杯

ruby docker dockerfile rake whenever
1个回答
1
投票

这是我上面列出的问题的解决方案。我在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

配置/ schedule.rb

  • 没有必要ENV.each
every 1.minutes do
  rake 'hello:start'
end

UPDATE

我已经创建了一个GitHub repository和一个Docker Hub repository来与社区分享这一进展。

© www.soinside.com 2019 - 2024. All rights reserved.