I am using 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 I run docker-compose up
, the sidekiq service can not start rightly.
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
A little bit more details in general on how to put Sidekiq into Docker in a Rails application. And for your reference, all the code is available on a GitHub repository.
Configure a Redis container
Sidekiq depends on Redis. So first of all, you need a Redis container to run alongside.
In your docker-compose.yml, add (as an example):
Dockerize Sidekiq
Share the same Dockerfile with your Rails app:
Update your docker-compose.yml
The environment file .env looks like this:
Also in your docker-compose.yml, add sidekiq to your Rails application's dependency list:
Add to your Gemfile
The sinatra gem is needed for the Sidekiq web UI (the dashboard shown below)
Configure Sidekiq
Add file config/initializers/sidekiq.rb:
Add a Sidekiq worker
Under directory app/workers/, add a file my_worker.rb:
That's it. Now you can submit a job in Rails, e.g., in a controller.
And the worker will pick up the job, and output a message in the log file.
Build and run with docker compose
Once everything is in place, you can build docker images and run your app with docker compose:
Test
Now open the following URL to submit a job:
And in the log file, you will see something like this:
Sidekiq dashboard
If you would like to use the Sidekiq dashboard, as below
You can add the route to your routes.rb
Hope it helps.
By the way, if you would like to find out how to dockerize your Rails application using docker compose, refer to docker compose documentation.
Try to mount the volumes. Your
docker-compose
file should look like this (with PosgtreSQL as database) :