Run Rails whenever with docker

Motivation

I tried to use whenever which executes Rails regularly, but I decided to do it with docker because it was difficult to manage environment variables and permissions when implementing it directly on Mac.

Prerequisites

The execution environment is as follows ・ Ruby 2.6.3 ・ Rails 5.2.4 · MySQL 8.0.19

Docker related

Dockfile, docker-compose.yml looks like this It's not much different from normal docker settings, but since we use whenever, we've added the settings for installing corn and running cron in the foreground.

FROM ruby:2.6.3
#ruby version specification

#gem installation
RUN apt-get update -qq && \
    apt-get install -y build-essential \ 
                       libpq-dev \        
                       nodejs

#cron installation
RUN apt-get install -y cron 

RUN mkdir /my_app

WORKDIR /my_app

COPY Gemfile /my_app/Gemfile
COPY Gemfile.lock /my_app/Gemfile.lock

RUN gem install bundler
RUN bundle install

COPY . /my_app

#Write crontab with whenever
RUN bundle exec whenever --update-crontab 

#Run cron in the foreground
CMD ["cron", "-f"] 

docker-compose.yml


version: '2'
services:
  db:
    image: mysql:8.0.19
    command: 
      --default-authentication-plugin=mysql_native_password
    volumes:
      - ./mysql-confd:/etc/mysql/conf.d
      - mysql-data:/var/lib/mysql    #For data persistence
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      # MYSQL_DATABASE: app_development
      MYSQL_USER: root
      # MYSQL_PASSWORD: password
      TZ: Asia/Tokyo
  app:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/my_app   
      - bundle:/usr/local/bundle   
    ports:
      - "3000:3000"
    links:
      - db

volumes:
  mysql-data:
  bundle:      #You don't have to rebuild after bundle install

whenever relationship

config/schedule.rb


#Rails because you need to start rails whenever.Use root
require File.expand_path(File.dirname(__FILE__) + "/environment")

#It does the environment variables nicely
ENV.each { |k, v| env(k, v) }

#File to write logs
set :output, error: 'log/crontab_error.log', standard: 'log/crontab.log'
set :environment, :development

#Every 2 minutes`sample_task`of`scheduled_task`To run
every 2.minutes do
  rake 'sample_task:scheduled_task'
  # runner "Test.yakisoba", :environment => :development #runner example
end

Command you want to execute

/lib/tasks/sample_task.rb


namespace :sample_task
  desc "scheduled_task"
  task scheduled_task: :environment do
     .....Function you want to execute
    end
  end
end

Recommended Posts

Run Rails whenever with docker
[Docker] Use whenever with Docker + Rails
Rails deploy with Docker
Run Pico with docker
Run Payara with Docker
Run TAO Core with Docker
[Docker] Rails 5.2 environment construction with docker
Rails + MySQL environment construction with Docker
Run lambda with custom docker image
Run SQL Server with Docker ToolBox
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Make JupyterLab run anywhere with docker
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Deploy to heroku with Docker (Rails 6, MySQL)
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
Run Ubuntu + ROS with Docker on Mac
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
Introducing Rspec with Ruby on Rails x Docker
Create jupyter notebook with Docker and run ruby
[Rails] How to build an environment with Docker
Deploy Line bot with rails5 + Docker + Heroku Note
Easy to display hello world with Rails + Docker
Docker x Rails 6 (memo)
Test run on rails
[Rails 6] RuntimeError with $ rails s
Explode Docker with WSL2
Use Puphpeteer with Docker
Handle devise with Rails
[Rails] Learning with Rails tutorial
Operate Emby with Docker
Use ngrok with Docker
[Rails] Test with RSpec
[Docker] Connection with MySQL
Php settings with Docker
[Rails] Development with MySQL
Getting Started with Docker
Supports multilingualization with Rails!
Disposable PHP with Docker
Double polymorphic with Rails
Install Composer with Docker
Run phpunit on Docker
Run old Vivado with Docker guest OS as CentOS 7.4
Bundle install with docker + rails6 does not reflect gem
Run Ruby on Rails RSpec tests with GitHub Actions
Error encountered with notes when deploying docker on rails
Run Android instrumentation unit tests with GitLab CI + Docker
Run RSpec, Brakeman, Rubocop, Rails Best Practices with overcommit
Run Mosquitto with Docker and try WebSocket communication with MQTT
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker + Rails] How to deal with Rails server startup failure
What is docker run -it?
Use GDAL with Python with Docker
[Rails 6] Dockerize existing Rails apps [Docker]
Run batch with docker-compose with Java batch
I made a development environment with rails6 + docker + postgreSQL + Materialize.