To run a Stimulus Reflex in Docker with Sidekiq and Redis
- Create a Dockerfile
FROM ruby:2.7.3
RUN curl https://deb.nodesource.com/setup_12.x | bash
RUN curl 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 && apt-get install -y nodejs yarn postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
- Create a Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.3'
- Create a Gemfile.lock
touch Gemfile.lock
- Create a docker-compose.yml file
version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
environment:
RAILS_ENV: "development"
REDIS_URL: "redis://redis:6379/12"
ports:
- "3000:3000"
depends_on:
- db
- Generate the project
docker-compose run web rails new . --force --database=postgresql
- Build the containers
docker-compose build
- User permission
sudo chown -R $USER:$USER .
- Update the database config/database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
- Create the database
docker-compose run web rake db:create
- Add sidekiq and gem
gem 'sidekiq'
gem 'stimulus_reflex'
- Update Gemfile.lock
docker-compose run web bundle install
- Add redis and sidekiq container
redis:
image: redis
volumes:
- ./tmp/db:/var/lib/redis/data
sidekiq:
build: .
command: 'bundle exec sidekiq'
volumes:
- .:/myapp
environment:
RAILS_ENV: "development"
REDIS_URL: "redis://redis:6379/12"
depends_on:
- redis
- Add active job queue config
config.active_job.queue_adapter = :sidekiq
- Add sidekiq config
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://redis.example.com:7372/12' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://redis.example.com:7372/12' }
end
- Install Stimulus
docker-compose run web rails stimulus_reflex:install
- Install Stimulus Reflex
docker-compose run web rails webpacker:install:stimulus
- Genrate Page Controller
docker-compose run web rails g controller Pages index
- Edit app/views/pages/index.html.erb
<a href="#"
data-reflex="click->Counter#increment"
data-step="1"
data-count="<%= @count.to_i %>"
> Increment <%= @count.to_i %></a>
- Add counter_reflex.rb in app/reflex folder and edit and add
class CounterReflex < ApplicationReflex
def increment
@count = element.dataset[:count].to_i + element.dataset[:step].to_i
end
end