Add Sidekiq Gem

Add the Sidekiq gem to the Gemfile and run bundle install

source 'https://rubygems.org'
# Other gems..

gem 'sidekiq', '~> 6.0.0'
Gemfile

Configuration

Once the gem is installed create a sidekiq.yml file under the config directory. Specify the desired concurrency and queue names (you can have multiple, i.e. default, high, low)

---
:concurrency: <%= ENV["CONCURRENCY"] || 25 %>
:queues:
 - default
# config/sidekiq.yml

Once the sidekiq.yml is created, you will need to setup the server and client initializers. Create a sidekiq.rb file under config/initializers that establishes Sidekiq's connection to Redis.

Sidekiq.configure_server do |config|
  config.redis = { url: ENV['REDIS_URL'] ||= 'redis://redis:6379' }
end

Sidekiq.configure_client do |config|
  config.redis = { url: ENV['REDIS_URL'] ||= 'redis://redis:6379' }
end
config/initializers/sidekiq.rb

Dockerfile

The last step is to setup a Dockerfile and docker-compose.yml file to bring the services up. You will need to define two services redis and sidekiq.

The redis service will pull a standard Redis image and run on port 6379 on the host. As part of the setup a password is added and the redis data is setup to store in the volume tmp/db on the host.

The sidekiq service builds its image from the Ruby project Dockerfile (i.e. build: .). This means we need to create a Dockerfile in the root of the Ruby project directory. We will use a Sinatra application as an example with a ruby:2.5.7-alpine image.

# Using the base ruby 2.5.7 image (no 2.5.5 alpine)
FROM ruby:2.5.7-alpine

# Update the installer
RUN apk update -qq && apk upgrade

# Install Deps
RUN apk add build-base \
	 openssh \
	 git


# Convenience variable to use for our app working directory
ENV APP /app

# Create that directory in the base image
RUN mkdir $APP
WORKDIR $APP

# Add Gemfile and Gemfile.lock to app directory
ADD Gemfile* $APP/

RUN gem install bundler --version '~> 2.0.0'
RUN bundle install

ADD . $APP

EXPOSE 4567
Dockerfile

This Dockerfile installs the necessary dependencies and setups an application called APP (mapping to /app) for our application. With this file available we can complete the docker-compose.yml file .

version: '3'
services:
 redis:
    image: redis:5.0-alpine
    ports: '6379:6379'
    command: >
      --requirepass change_me_password
    volumes:
      - /tmp/db:/var/lib/redis/data
  sidekiq:
    build: .
    # If you have any environment files to require 
    # add the Sidekiq -r flag to require them 
    command: 'sidekiq -c config/sidekiq.yml'
    volumes:
      # Must be current working direcotry for Gemfile
      - .:/app
    environment:
      REDIS_URL: "redis://redis:6379/12"
    depends_on:
      - redis
docker-compose.yml

Run docker-compose up and your services should be running.