Develop RubyOnRails without installing ANY dependencies with Docker

In seconds, have Docker to give you a full-fledge RubyOnRails development environment up & running. Stop trashing your machine with dependencies that get old quickly or relying on slow virtual machines.

Introduction

Setting up a development environment may be a pain in the neck. And there's a limit in what package managers can do.

Known is the case of postgresql dependency halt
when you try a bundle install
(yay, it rhymes!).

Docker can help to have, in seconds, an Interactive shell that allow you to Build and Run your Rails code.

If you haven't a clue of what Docker Interactive Shell is about, please read this first: Docker: have a Ubuntu development machine within seconds, from Windows or Mac.

If you are already familiar with running docker containers and attaching volumes to it, I will now give you the "Golden Line".

The Golden Line

I call the "Golden Line" that single-liner that get you up and running very quickly when you google for it and find an article like this.

Behold, the "Golden Line" for a Docker RubyOnRails Development Environment:

For windows

Ensure your C: drive is shared with dockers, in docker settings, then run the command below in your Docker CLI Powershell:

docker run `
--name "railsdev" `
-p 3000:3000 `
-v C:\Users\hudson\Workspaces:/src `
-e "DB_HOST=192.168.1.166" `
-t -i `
hudsonmendes/railsdev

For macs

Run this in your Terminal:

docker run \
--name "railsdev" \
-p 3000:3000 \
-v /Users/hudson/Workspaces:/src \
-e "DB_HOST=$(ifconfig en0 | awk '/ *inet /{print $2}')" \
-t -i \
hudsonmendes/railsdev

Understanding the Golden Line

What do you need to know about the golden line?

  • "-p 3000:3000" manually maps the port 3000 of your machine (host) to the container's port 3000. This allows you to reach your app after you run your rails s
  • "-v C:\Users\hudson\Workspaces:/src" maps your local folder to the container's volume "/src", this is how you edit the code in your IDE and how you see it in the
  • the "-e HOST_IP=" injects an environment variable named HOST_IP into your docker container to make it possible to connect to a database, or any other service, installed not inside docker, but in your host machine. Replace your "localhost" in your db config by this environment variable.
  • No need to run the "/bin/bash", because it's already entrypoint of the image
  • The image is one I created and published, and that works for my normal setup: rails + postgresql. See here the Dockerfile on Github to see how I set it up, and feel free to fork it/create your own. You will need one of these to develop in a container.
  • I also made this image available on Dockerhub for convenience (yours, and mine)

My Dockerfile

Leaving a copy of my Dockerfile here:

FROM ruby:latest
ENV RAILS_VERSION 5.0.1
ENV DB_HOST DONT_FORGET_TO_TELL_THE_DB_HOST
RUN apt-get update && \
apt-get install -y nodejs --no-install-recommends && \
rm -rf /var/lib/apt/lists/* && \
gem install rails --version "$RAILS_VERSION"
VOLUME ["/src"]
EXPOSE 3000 3000
WORKDIR /src
ENTRYPOINT ["/bin/bash"]