How to put sidekiq in Docker in a rail application?

I use rails, sidekiq and docker.

My docker-compose.yml file

sidekiq: build: . command: bundle exec sidekiq -C config/sidekiq.yml links: - db - redis 

config / sidekiq.yml file

 :pidfile: ./tmp/pids/sidekiq.pid :logfile: ./log/sidekiq.log :queues: - default 

After starting docker-compose up , the sidekiq service cannot start correctly.

 sidekiq_1 | No such file or directory @ rb_sysopen - /testapp/tmp/pids/sidekiq.pid sidekiq_1 | /usr/local/bundle/gems/sidekiq-3.5.3/lib/sidekiq/cli.rb:365:in `initialize' sidekiq_1 | /usr/local/bundle/gems/sidekiq-3.5.3/lib/sidekiq/cli.rb:365:in `open' sidekiq_1 | /usr/local/bundle/gems/sidekiq-3.5.3/lib/sidekiq/cli.rb:365:in `write_pid' sidekiq_1 | /usr/local/bundle/gems/sidekiq-3.5.3/lib/sidekiq/cli.rb:42:in `parse' sidekiq_1 | /usr/local/bundle/gems/sidekiq-3.5.3/bin/sidekiq:12:in `<top (required)>' sidekiq_1 | /usr/local/bundle/bin/sidekiq:16:in `load' sidekiq_1 | /usr/local/bundle/bin/sidekiq:16:in `<main>' testapp_sidekiq_1 exited with code 1 
+6
source share
2 answers

Try installing volumes. Your docker-compose file should look like this (using a PosgtreSQL database):

 web: build: . volumes: - .:/myapp links: - db - redis ports: - "3000:3000" command: bundle exec rails server -b 0.0.0.0 sidekiq: build: . volumes: - .:/myapp links: - db - redis command: bundle exec sidekiq db: image: postgres redis: image: redis 
+12
source

A bit more detail overall about how to put Sidekiq in a Docker in a Rails application. And for your reference, all the code is available on the GitHub repository .

Configure Redis Container

Sidekiq is dependent on Redis . Therefore, first of all, you need to use the Redis container.

In your docker-compose.yml add (as an example):

 redis: image: redis:4.0-alpine 

Dockerize sidekiq

Share the same Docker file with your Rails application:

 FROM ruby:2.3.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile ADD Gemfile.lock /myapp/Gemfile.lock RUN bundle install ADD . /myapp 

Update the docker-compose.yml file

 sidekiq: build: . command: bundle exec sidekiq depends_on: - redis volumes: - .:/myapp env_file: - .env 

The .env environment file is as follows:

 JOB_WORKER_URL=redis://redis:6379/0 

Also in your docker-compose.yml file add sidekiq to your Rails application dependency list:

 web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" depends_on: - db - sidekiq env_file: - .env 

Add to Your Gemfile

 gem 'sidekiq' gem 'sidekiq-scheduler' gem 'sidekiq-unique-jobs' gem 'sinatra', require: nil 

Synthetic pearls are required for the Sidekiq web interface (toolbar shown below)

Customize Sidekiq

Add config / initializers / sidekiq.rb file:

 sidekiq_config = { url: ENV['JOB_WORKER_URL'] } Sidekiq.configure_server do |config| config.redis = sidekiq_config end Sidekiq.configure_client do |config| config.redis = sidekiq_config end 

Add employee Sidekiq

In the application / directory worker / add the file my_worker.rb:

 class MyWorker include Sidekiq::Worker def perform(who, message) logger.info "Message from #{who} is #{message}" end end 

What is it. Now you can send the task to Rails, for example, to the controller.

 MyWorker.perform_async(who, message) 

And the worker will complete the task and display the message in the log file.

Build and run with docker compose

Once everything is in place, you can create docker images and launch the application using docker:

 docker-compose build docker-compose up $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0515ac60a8b hellosidekiq_web "bundle exec rails..." 23 minutes ago Up 23 minutes 0.0.0.0:3000->3000/tcp hellosidekiq_web_1 080e33963e3a hellosidekiq_sidekiq "bundle exec sidekiq" 23 minutes ago Up 23 minutes hellosidekiq_sidekiq_1 80d1c03f0573 redis:4.0-alpine "docker-entrypoint..." 4 days ago Up 23 minutes 6379/tcp hellosidekiq_redis_1 5915869772e4 postgres "docker-entrypoint..." 4 days ago Up 23 minutes 5432/tcp hellosidekiq_db_1 

Test

Now open the following URL to submit the job:

 http://localhost:3000/job/submit/John/Prepare%20ye%20the%20way 

And in the log file you will see something like this:

 sidekiq_1 | 2017-11-13T17:08:45.876Z 1 TID-qw47g MyWorker JID-b7b6d39b0d5193cd01e97cb1 INFO: Message from John is Prepare ye the way 

Sidekiq Dashboard

If you want to use Sidekiq panel as shown below

enter image description here

You can add a route to your routes. rb

 require 'sidekiq/web' require 'sidekiq-scheduler/web' Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html mount Sidekiq::Web => '/sidekiq' get 'job/submit/:who/:message', to: 'job#submit' end 

Hope this helps.

By the way, if you want to learn how to re-resize your Rails application using docker layout, check with docker for documentation .

+2
source

All Articles