Ruby on Rails DevContainer setup that includes Ruby, Ruby on Rails, Selenium, PostgreSQL, Redis, Memcached, and MeiliSearch.
Softwares:
- Ruby version
3.3.4 - NodeJS version
20.15.0(or later) - Yarn version
1.22.22(or later) - Zsh
- Git
- Vim
- Selenium
- PostgreSQL
- Redis
- Memcached
- MeiliSearch
Extensions:
- Postman
- Rails DB Schema
- Rails I18n
- Rails Partial
- Tailwind CSS IntelliSense
- vscode-gemfile
- GitLens — Git supercharged
- Ruby ERB::Formatter
- Run on Save
- ERB Linter
- Stimulus LSP
- Live Preview
- SQLTools
- SQLTools PostgreSQL/Cockroach Driver
- vscode-icons
- Git Blame
- Shopify LSP
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.
This DevContainer setup does the following for you while building the image:
- Runs
gem update --system - Runs
bundle - Runs
yarn - Runs
rails db:create db:migrate db:seed
You can customize this behavior inside .devcontainer/setup.sh file.
This DevContainer setup assumes that you are using erb-lint, erb-formatter, and i18n-tasks gems, as it sets up formatting commands using Run on Save VSCode extension to format erb, css, and yml files. See devcontainer.json lines 83-106 for more details.
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: 5432This is required because the docker service for PostgreSQL is named db inside .devcontainer/docker-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.
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 redis inside .devcontainer/docker-compose.yml, and you should access it by its name.
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.
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 meilisearch inside .devcontainer/docker-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.
- 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
.devcontainerdirectory - 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 :)