DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Michiel Sikkes
Michiel Sikkes

Posted on • Originally published at michiels.io on

Introducing a Rails 5.1 development environment via Docker

I’m working on something new: an easy to set up Rails 5.1 development environment in Docker, configured via docker-compose. I’m maintaining a template for this on GitHub here:michiels/rails-docker-compose. Follow the README there for instructionson setting it up on your app.

Why?

Why am I doing this? I’d like all members in our team atFirmhouse to not have to think about local dependencies anymore when working on one of the many apps we develop. Getting set up with an app should be a matter of seconds.

Having a development environment per app also helps cleaning out any databases and Sidekiq queues and gem interdepencies. So you always have fresh and clean environment and you don’t have to recompile Ruby for each differeing version on your workstation.

What it contains:

I want to maintain the repository so it always supports a Rails development environment for:

  • Latest Ruby on Rails
  • Latest Ruby version
  • PostgreSQL
  • Redis (for Sidekiq)
  • RubyGems for installing gems (standard Rails)
  • Yarn for installing JS dependencies (standard Rails)
  • System tests with headless Chrome

How does it work for your app?

You can read more detailed instructions in the README, but here’s the gist:

First, you copy over the Dockerfile, docker-compose.yml file and slightly modify your database.yml and docker-compose.yml.

Then, you set up the app’s database via:

  $ docker-compose run --rm web rails db:setup
Enter fullscreen mode Exit fullscreen mode

Finally, it’s super easy to start a local Rails development environment:

  $ docker-compose up
Enter fullscreen mode Exit fullscreen mode

Browse to http://localhost:3000/ and your app is up and running with Rails, PostgreSQL, Redis, and Sidekiq.

Bonus hint: Keep a local development session open for generating controllers and models, and running migrations and tests. For example:

$ bin/devconsole
# rails g controller homepage
# rails db:migrate
# rails test
# rails test:system
Enter fullscreen mode Exit fullscreen mode

Try it, please!

I’d love to see this set up work in your app. Let me know if it works out or what doesn’t.

Top comments (10)

Collapse
drbragg profile image
Drew Bragg

This is fantastic!

suggestion => don't use Ruby 2.5 (yet), there are still some gems out there with compatibility issue with 2.5, chief among them being devise.

I'm sure this will get fixed in the (very) near future but just a suggestion.

Collapse
michiels profile image
Michiel Sikkes Author

Thanks Drew! Very late reply, but we've been using Ruby 2.5 in our Docker setup successfully for the past few months. So everything seems nice!

Collapse
drbragg profile image
Drew Bragg

Awesome!

Collapse
jschoolcraft profile image
Jeff Schoolcraft

How does this setup support system tests with headless chrome? Are they run on host?

Collapse
michiels profile image
Michiel Sikkes Author

System tests are run inside the container. Headless chrome is installed inside the container. So you can just open the shell in the container and run:

rails test:system
Collapse
jschoolcraft profile image
Jeff Schoolcraft

I'm guessing it and bin/devconsole come from railsdockercompose_web but I can't pull it:

Status: Downloaded newer image for redis:latest
Pulling sidekiq (railsdockercompose_web:latest)...
ERROR: pull access denied for railsdockercompose_web, repository does not exist or may require 'docker login'

I searched for it on hub.docker.com but didn't get any results.

Collapse
jschoolcraft profile image
Jeff Schoolcraft

Just wasn't seeing where headless chrome was being installed. Couldn't see it in the Dockerfile or in compose. Am I missing something?

Thread Thread
michiels profile image
Michiel Sikkes Author

I forgot to push some commits after publishing the post. The latest master is updated with everything fixed from your previous comments.

Everything is in the local repo. You could also specify an "image" in docker-compose.yml if you intend to build/push your repo to a Docker CI.

However, the latest docker-compose.yml is for local standalone development that doesn't depend on a container image hosted somewhere else.

Collapse
bizzibody profile image
Ian bradbury

Brilliant. I will try this.
+1

Collapse
michiels profile image
Michiel Sikkes Author

Nice! I’m available for questions, help, or improvements!

🌚 Life is too short to browse without dark mode