Create ruby on rails app using docker
Today we will learn how to create a ruby on rails using docker and docker-compose. I was waiting to publish this tutorial from long time. This tutorial will help in creating ruby on rails app using docker from scratch.
- Docker and docker-compose must be installed or use this tutorial to install docker
- I am using MacBook and i have installed docker for mac
Create application directory
Firstly create an application directory. I am using MacBook for this tutorial. I mostly use docker directory to create docker projects in my home directory.
Now create a Gemfile in our project directory and add the following contents in the file
gem 'rails', '4.2.0'
Create empty Gemfile.lock
Now create a empty Gemfile.lock in the project directory using touch file.
Now we will create a file named Dockerfile in the project directory in order to build docker image. Create a file and add this:
FROM ruby:2.2.0 MAINTAINER Chetan Kapoor ([email protected]) RUN apt-get update -qq RUN apt-get install -y build-essential libpq-dev RUN mkdir /app WORKDIR /app ADD Gemfile /app/Gemfile ADD Gemfile.lock /app/Gemfile.lock RUN bundle install ADD . /app
Create docker-compose.yml file
Now we will create the docker-compose.yml file. Create a file and add this in the file:
postgres: image: postgres web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/app ports: - "3000:3000" links: - postgres
Create the rails app
Now we will create the rails app using docker-compose. Execute the following command to create rails app:
docker-compose run web rails new . --skip-bundle --database=postgresql
The docker-compose run command will create a rails app in the current directory. Once the docker-compose finishes you can see rails app by executing following command:
Build the app
Now we will build our app using docker-compose. Before open the Gemfile and uncomment the following line:
gem 'therubyracer', platforms: :ruby
Then execute the following command to build the app.
We need to change config for database connection. Open config/database.yml with your favourite editor and replace the lines with following:
development: &default adapter: postgresql encoding: unicode database: postgres pool: 5 username: postgres password: host: postgres test: <<: *default database: web_test
Start the rails app with docker-compose
Now we can start our rails app using docker-compose. Docker-compose will spin up web and postgres container. Just execute the following command:
docker-compose up -d
View the app
Now our app is up and running, we can view the app in the browser. If you are creating app on locally then go to