iTranslated by AI
Complete Your Ruby on Rails Development Environment with Docker and IntelliJ Remote SDK
I had the opportunity to develop a Web API with Ruby on Rails, but I usually only work with TypeScript and don't have Ruby installed on my local machine. I explored ways to complete the development without installing Ruby locally, and it seemed like IntelliJ's Remote Ruby Interpreter could be useful. This article introduces how to set up a Rails development environment using a Remote Interpreter. For VSCode, the same can likely be achieved with Remote Containers.
Environment
| Tool/Library | Version |
|---|---|
| MacOS | Catalina |
| Ruby | docker hub ruby: 2.7 |
| Ruby on Rails | 6.1 |
| IntelliJ | 2020.3 |
| Docker Desktop | 3.1.0 |
Content
To Do
- Create
docker-compose.ymlandrails newa sample project - Configure IntelliJ to recognize the Ruby SDK on Docker
- Configure IntelliJ to recognize RubyGems
We will proceed referencing Quickstart: Compose and Rails| Docker.
Not To Do
- Enable debugging in IntelliJ
- Enable running tests in IntelliJ
Tests will be left to execution via the command line terminal.
Create docker-compose.yml and rails new a sample project
To prepare a sample project, Ruby and Rails are immediately needed. Let's proceed by following the official guide.
First, prepare a Dockerfile in an empty directory where you want to create the project. I'm proceeding with VSCode for this part.
FROM ruby:2.7
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - &&\
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list &&\
apt-get update -qq && apt-get install -y nodejs yarn postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
# 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
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
Next. When creating a Docker container, it seems to pull the Gemfile from the host to install Rails, so we will create Gemfile and an empty Gemfiile.lock.
source 'https://rubygems.org'
gem 'rails', '~>6'
touch Gemfile.lock
entrypoint.sh also seems necessary.
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
Finally, the docker-compose.yml. We will follow the official content.
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
ports:
- "3000:3000"
depends_on:
- db
In this state, running docker-compose run web rails new will generate the sample project.
docker-compose run --no-deps web rails new . --force --database=postgresql
Once the Ruby on Rails files are generated in the directory where the command was executed, you are done.
Configure IntelliJ to recognize the Ruby SDK on Docker
To use the Ruby SDK, configure IntelliJ's SDK to configure a Remote Ruby Interpreter. Create a new project by going to Menu Bar > File > New > New Project. Although we have already created the Rails file group in the previous step, we take this step not to open an existing project but for IntelliJ to recognize it as a "Rails project."
From here, select Project SDK > Add Ruby SDK > New Remote... as shown in the image below.
Adding Remote SDK
Next, the Ruby interpreter settings screen will appear, configure it as follows:
- Docker compose
- Server: Docker (default settings after New... are OK)
- Configuration files: The
docker-compose.ymlcreated above - Service:
web - Environment variables:
<empty> - Ruby or version manager path:
ruby
Once configured, Ruby SDK and Rails should be set up, so click Next and set the created project name and location. Then create the project. You might be asked whether to overwrite existing Rails settings; choose "Do not overwrite."
Finally, press Ctrl twice to bring up Run Anything and run bundle install. This will install gems into IntelliJ via Docker Compose's Ruby. As a result, code jumping and require resolution in the editor will become available.
This completes the setup, allowing you to develop Rails applications with IntelliJ without installing Ruby on your local machine.
Launching the Rails App
Although it's slightly off-topic, since we've come this far, let's go ahead and launch the app following Docker's official guide, Connect the database.
Modify config/database.yml with the following content...
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
Then, start Docker.
docker-compose up
You'll probably encounter a Webpacker configuration file not found error, so run the following. This is where yarn becomes necessary.
docker-compose run --no-deps web bundler exec rails webpacker:install
After installation, start it.
docker-compose up
It should launch without issues. There's still a step to create the database, so run it in a separate terminal.
docker-compose run web bundle exec rake db:create
If you see the welcome page at localhost:3000, it's a success.
Summary
We added a Remote SDK to make Rails 6 development on Docker possible with IntelliJ. This allows development without installing Ruby on a Mac. Furthermore, we corrected the slightly incomplete official guide for using Rails 6 with Docker and demonstrated how to get it running. I hope this was helpful.
Discussion