zzJZzz / ruby-on-rails-devcontainer

Ruby on Rails devcontainer setup that includes Ruby, Ruby on Rails, PostgreSQL, Redis, MeiliSearch, Memcached, and Firefox to run system tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ruby on Rails Devcontainer

Ruby on Rails devcontainer setup that includes Ruby, Ruby on Rails, PostgreSQL, Redis, MeiliSearch, Memcached, and Firefox to run system tests

Pre-requisits

What are we setting up in this devcontainer?

Softwares:

  • Latest version of Git version control system
  • Version 3.2.0 of Ruby programming language
  • Version 19 of NodeJS JavaScript runtime environment
  • PostgreSQL database based on the latest official docker image
  • Redis in-memory key-value store based on the latest official docker image
  • OpenSearch search engine based on the latest official docker image
  • Sidekiq in-memory key-value store based on the latest official docker image
  • Firefox browser to run system tests
  • postgresql-client

Extensions:

How to use?

Copy the .devcontainer directory into your Ruby on Rails project root directory and open it inside VSCode, then you will be able to use devcontainers as documented here.

Things to be aware of

Automatic setup

This devcontainer setup does the following for you while building the image:

  • Runs gem update --system
  • Runs bundle install
  • Runs rails db:prepare
  • Adds .devcontainer/launch.json file to .vscode directory to be able to run debugging inside VSCode

You can customize this behavior inside .devcontainer/setup.sh file.

PostgreSQL

You need to set the host and port properties for your development and testing databases in config/database.yml like this:

development:
  <<: *default
  database: ror_project_development

  username: postgres
  password: postgres
  host: db
  port: 5432

test:
  <<: *default
  database: ror_project_test

  username: postgres
  password: postgres
  host: db
  port: 5432

This is required because the docker service for PostgreSQL is named as db inside .devcontainer/docket-compose.yml, and you should access it by its name.

If you changed the username and/or password inside config/database.yml, then you will need to change them inside .devcontainer/devcontainer.json file to get sqltools extensions works properly.

Redis

To connect to Redis in the development environment, you need to instantiate the connection like this:

redis = Redis.new(url: ENV['REDIS_URL'] || 'http://redis:6379')

You should define REDIS_URL in your production environment, while in development redis://redis:6379 will be used. This is required because the docker service for Redis is named as redis inside .devcontainer/docket-compose.yml, and you should access it by its name.

MeiliSearch

Inside your config/initializers/meilisearch.rb initializer, make sure to set the meilisearch_url and meilisearch_api_key properties like this:

MeiliSearch::Rails.configuration = {
  meilisearch_url: ENV['MEILISEARCH_HOST'] || 'http://meilisearch:7700',
  meilisearch_api_key: ENV['MEILISEARCH_KEY'] || 'LOCAL_TEST_KEY'
}

You should define MEILISEARCH_HOST and MEILISEARCH_KEY in your production environment, while in development http://meilisearch:7700 and LOCAL_TEST_KEY will be used. This is required because the docker service for MeiliSearch is named as meilisearch inside .devcontainer/docket-compose.yml, and you should access it by its name.

If you changed the meilisearch_api_key to something different than LOCAL_TEST_KEY, make sure to update .devcontainer/docker-compose.yml also.

Memcached

Make sure to change config.cache_store = :memory_store to config.cache_store = :mem_cache_store inside config/environments/development.rb. Also, you will need to enable caching in development by running rails dev:cache command.

No need to specify the Memcached server as it is already defined inside .devcontainer/Dockerfile like this ENV MEMCACHE_SERVERS=memcached:11211.

Firefox

To use Firefox in your system tests instead of Chrome, you will need to change using: :chrome to using: :headless_firefox inside test/application_system_test_case.rb file.

Notes

  • This is just a development setup, make sure to setup these services in your production environment also if you are using them
  • Rebuild your devcontainer image when you change any file inside .devcontainer directory
  • Take a look at the installed extensions and modify the list as required
  • This could be used in GitHub Codespaces easily with a single click
  • Feel free to submit any change you feel it is benefitial to this repo :)

About

Ruby on Rails devcontainer setup that includes Ruby, Ruby on Rails, PostgreSQL, Redis, MeiliSearch, Memcached, and Firefox to run system tests


Languages

Language:Dockerfile 92.0%Language:Shell 8.0%