In this tutorial, I will show you how to Dockerize Ruby on Rails app.
You’ve probably already used many of the applications that were built with Ruby on Rails: Basecamp, GitHub, Shopify, Airbnb, Twitch, SoundCloud, Hulu, Zendesk, Square, Highrise, Cookpad. Those are just some of the big names, but there are literally hundreds of thousands of applications built with the framework since its release in 2004.
Ruby on Rails is open source software, so not only is it free to use, but you can also help make it better. More than 4,500 people already have contributed code to Rails. It’s easier than you think to become one of them.
Read Also:
Sample App
I am using a sample rails app for this demonstration. You can freely use this sample app for testing purposes.
git clone https://github.com/chetankapoor/sample_rails.git
Dockerizing Ruby on Rails app
Now we will start dockerizing the Ruby on Rails app. I am using a sample rails app here. First of all add Dockerfile in your app directory.
vim Dockerfile
Then paste the following content into Dockerfile:
FROM ruby:2.4-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY Gemfile /usr/src/app/
RUN apk update && apk add --virtual build-dependencies build-base gcc sqlite-dev nodejs
RUN bundle install
COPY . /usr/src/app
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Build your app
After creating Dockerfile its time to build your app. You can use docker build command to build the docker image. We will also tag our image with -t option.
docker build -t sample_rails .
Then you can see your images with the following command.
docker images
Test your Image
Now its time to test your docker image.
docker run -it -p 3000:3000 sample_rails
Then Open your browser and open http://localhost:3000
You will see Hello World in your browser.
Please let me know if you like this tutorial.