Skip to content

Instantly share code, notes, and snippets.

@otaviomedeiros
Last active November 19, 2021 21:41
Show Gist options
  • Save otaviomedeiros/fb8eda4962da393f7c994c1afd09785a to your computer and use it in GitHub Desktop.
Save otaviomedeiros/fb8eda4962da393f7c994c1afd09785a to your computer and use it in GitHub Desktop.
Ruby on Rails with Sidekiq and docker compose
PORT=3000
REDIS_PORT=6379
SIDEKIQ_REDIS_URL=redis://redis:6379/0
APP_DATABASE_HOST=database
APP_DATABASE_PORT=5432
APP_DATABASE_NAME=app_development
APP_DATABASE_USERNAME=postgres
APP_DATABASE_PASSWORD=postgres
# config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV['APP_DATABASE_HOST'] %>
database: <%= ENV['APP_DATABASE_NAME'] %>
username: <%= ENV['APP_DATABASE_USERNAME'] %>
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
development:
<<: *default
test:
<<: *default
database: app_test
production:
<<: *default
version: "3.3"
services:
app:
build: .
env_file:
- .env
volumes:
- ".:/app"
ports:
- ${PORT}:${PORT}
depends_on:
- database
database:
image: postgres:13.2
restart: always
environment:
POSTGRES_PASSWORD: postgres
ports:
- ${APP_DATABASE_PORT}:${APP_DATABASE_PORT}
volumes:
- "pg_volume:/var/lib/postgresql/data"
redis:
image: redis
ports:
- ${REDIS_PORT}:${REDIS_PORT}
sidekiq:
build: .
depends_on:
- database
- redis
volumes:
- .:/app
env_file:
- .env
command: bundle exec sidekiq
volumes:
pg_volume:
driver: local
FROM ruby:2.7.2
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y build-essential nodejs yarn
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
CMD rm -f tmp/pids/server.pid && bin/rails server -b '0.0.0.0'
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.2'
gem 'rails', '~> 6.1.4', '>= 6.1.4.1'
# config/routes.rb
require 'sidekiq/web'
Rails.application.routes.draw do
mount Sidekiq::Web, at: '/sidekiq'
end
# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { url: ENV['SIDEKIQ_REDIS_URL'] }
end
Sidekiq.configure_client do |config|
config.redis = { url: ENV['SIDEKIQ_REDIS_URL'] }
end
@otaviomedeiros
Copy link
Author

otaviomedeiros commented Nov 19, 2021

Create initial files:

touch docker-compose.yml Dockerfile Gemfile Gemfile.lock .env

Copy contents of the corresponding files above.

Create new rails app:

docker-compose run --no-deps app rails new . --force --database=postgresql

Add gem 'sidekiq' to Gemfile

touch config/initializers/sidekiq.rb config/routes.rb config/database.yml

Copy contents of the corresponding files above.

Build docker images and install webpacker:

docker-compose build
docker-compose run app bundle exec rails webpacker:install

Create the database:

docker-compose up -d database
docker-compose run app rake db:create

Run the app:

docker-compose up -d

The app is running on localhost:3000. The Sidekiq web interface can be accessed at http://localhost:3000/sidekiq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment