I have been using Vagrant for a while in order to develop Ruby applications in an isolated environment, and it has served me well. But I wanted to give Docker a try! Especially since they have released Docker for Windows. Previously we only had the Docker Toolbox that included a virtual machine running TinyCore with Docker installed.
NOTE: In order to be able to use Docker for Windows, you will need to have the Hyper-V feature installed.
Lets get things set up
And now we need to run the following command:
docker run -it --rm -v "D:\Projects\wordpress":/usr/src/app -w /usr/src/app rails:5 rails new my-awesome-app --skip-bundle
It should now have created the rails app in D:\Projects\wordpress\my-awesome-app. We can now open this folder in Atom.
Now we have our Rails app…what next?
Now we need to create our Dockerfile so we can run our rails app on a specific version of Ruby. In this example I’ll use Ruby 2.3.1. This is the Dockerfile that is put in to the root of our rails project.
FROM ruby:2.3.1 RUN apt-get update -qq && apt-get install -y build-essential # for postgres RUN apt-get install -y libpq-dev # for nokogiri RUN apt-get install -y libxml2-dev libxslt1-dev # for a JS runtime RUN apt-get install -y nodejs ENV APP_HOME /myapp RUN mkdir $APP_HOME WORKDIR $APP_HOME ADD Gemfile* $APP_HOME/ RUN bundle install ADD . $APP_HOME
Now that we have our Dockerfile, we can build our Docker container by running the following command:
docker build -t my-awesome-app .
Then we can run our newly built Docker container with:
docker run -p 3000:3000 my-awesome-app rails s -p 3000 -b 0.0.0.0