SlideShare a Scribd company logo
HOW TO
DOCKERIZE
RAILS
APPLICATION
: COMPOSE
AND RAILS
TUTORIAL


https://www.bacancytechnology.com/
Introduction
A curious developer and tech enthusiast
never miss an opportunity to learn a little
more every day! I can absolutely relate to
this urge to learn. So, keeping your
curiosity in mind, we are back with
another Rails tutorial on how to dockerize
rails applications with the help of Docker
Compose.


Hoping that you are familiar with what is
Docker and why do we need Docker.


Let’s get started with the app
development and dockerizing it.
Create a Rails
App
Fire the following commands to create a
rails app.


mkdir ~/projects/noteapp
cd ~/projects/noteapp
Prerequisites:
Dockerize
Rails
Application
Install Docker Community Edition
Install Docker Compose
As we are implementing Docker Compose,
make sure about the following installations
before getting started.
Create a
Dockerfile
The Dockerfile is the foundation of any
Dockerized app. It contains all the
instructions for building the application
image. You can set this up by installing
Ruby and all of its dependencies. The
Dockerfile consists of the following
instructions.
FROM ruby:2.3.0
RUN apt-get update -qq && apt-get
install -y build-essential libpq-dev nodejs
RUN mkdir /noteapp
WORKDIR /noteapp
ADD Gemfile /noteapp/Gemfile
ADD Gemfile.lock /noteapp/Gemfile.lock
RUN bundle install
ADD . /noteapp
// Dockerfile
Dockerfile will keep the app code inside
an image, building a container with
Bundler, Ruby, and other dependencies.
Therefore in the root directory of the
application, create a new Dockerfile using
the command touch Dockerfile and put
the content of the above dockerfile inside
it.
Explanation
FROM ruby:2.3.0: Tells Docker to use
the prebuilt Ruby image. There are
several choices, but this project uses
the ruby:2.3.0 image.
RUN: To run commands. Here, RUN is
for installing different software pieces
with Apt.
WORKDIR: For stating the base
directory from where all the
commands are executed.
ADD: For copying files from the host
machine to our container.
Create a
Gemfile
Next, open the editor and create a
bootstrap Gemfile that loads Rails.
source 'https://rubygems.org'
gem 'rails', '~>5.0.0'
Create an empty Gemfile.lock file to build
our Dockerfile.
touch Gemfile.lock
// gemfile
Dockerize Rails App: Add
Portability, Modularity, and
Scalability to your app
Contact Bacancy and hire Rails developer to
dockerize your rails application.
Define
Services
Using Docker
Compose
Finally, moving towards the most important
section. The docker-compose.yml file will
consist of services needed for your app (web
application and DB), for getting each other’s
Docker image, and the config for connecting
them and making it visible on the port.
version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD:
password
MYSQL_DATABASE: noteapp
MYSQL_USER: appuser
MYSQL_PASSWORD: password
// docker-compose.yml
ports:
- "3307:3306"
app:
build: .
command: bundle exec rails s -p 3000 -b
'0.0.0.0'
volumes:
- ".:/noteapp"
ports:
- "3001:3000"
depends_on
- db
links:
- db
Build the
Project
Now build the skeleton of the rails
application with the help of docker-
compose run.


docker-compose run app rails new . --force
--database=mysql
compose– builds the image for the app
service, which we have to define inside
our docker-compose.yml
runs rails new – using that image it runs
the app inside a new container
database=mysql– to define the database
Your application should be created after the
command is successfully executed. List the
files using ls -l
Database
Connection
In this section, we will connect the database
as rails wants a database to be running on
the localhost.


We will also alter the database and
username for aligning it with the defaults
by the MySQL image. When we run the
docker-compose command first, it will
create a DB container that downloads the
MySQL database image and creates a DB
based on the environment variables set in
the docker-compose.yml file.


By now, we have a database container, and
app structure created. We need to edit the
config/database.yml file and set the
configurations from the environment
variables.


Replace the contents of config/database.yml
with the following:
version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: noteapp
MYSQL_USER: appuser
MYSQL_PASSWORD: password
ports:
- "3307:3306"
app:
build: .
command: bundle exec rails s -p 3000 -b
'0.0.0.0'
// docker-compose.yml
volumes:
- ".:/noteapp"
ports:
- "3001:3000"
depends_on:
- db
links:
- db
environment :
DB_USER: root
DB_NAME: noteapp
DB_PASSWORD: password
DB_HOST: db
After setting up the docker-compose.yml,
run the docker-compose build command to
build an image for the app and install all the
required gems.
Run the below command for database
creation.


docker-compose run --rm app rake
db:migrate
Before creating any migrations/models, let’s
do a docker-compose up to start both app
and database services and boot the
application after making the changes in
database.yml.


We can see that rails is running on port
3000 in the container after the command is
successfully executed. But, that’s not the
port on the host, so we won’t be able to
access it on the browser. As per docker-
compose, we have exposed the port on the
localhost from 3000 to 3001; therefore, it
should be available on localhost:3001.
Once you are done with the app running on
the browser, create a model and perform
the migration using these commands in a
different console of the project directory.
docker-compose run --rm app rails g
scaffold note title body:text
docker-compose run --rm app rake
db:migrate
Now, we can access the application on port
3001- localhost:3001/notes and perform
actions on the application.
Summary:
How to
Dockerize
Rails
Application
mkdir ~/projects/noteapp
cd ~/projects/noteapp
Create Gemfile and empty Gemfile.lock
(content is given above)
Create Dockerfile (content is given
above)
Create docker-compose.yml (content is
given above)
docker-compose run app rails new . –
force –database=mysql
Make changes in config/database.yml
docker-compose build
docker-compose up
http://localhost:3001
docker-compose run –rm app rails g
scaffold note title body:text
docker-compose run –rm app rake
db:migrate
http://localhost:3001/notes


Watch the video tutorial on how to
dockerize rails application as well.
Source Code: dockerize-
rails-app
You can also clone the code and go through
the project. Here’s the source code of the
repository: dockerize-rails-app


Commands: Stop,
Restart, and Rebuild the
Application
To stop the application
docker-compose down
To restart the application
docker-compose up
To rebuild the application
Rebuilding the application is a must when
you’re trying different configs and altering
the Gemfile or Compose file.
➡Sometimes only docker-compose up –
build is enough.
➡But, if you want to rebuild the entire app
fully, then use docker-compose run app
bundle install, followed by docker-compose
up –build for synchronizing changes
between the Gemfile.lock and the host.
Conclusion
That’s it for the tutorial: how to dockerize
rails application using Docker compose. I
hope the tutorial was helpful to you for
building your own demo app and exploring
more.


Visit the Ruby on Rails tutorials page for
similar tutorials, where you can explore
your interests and play around with the
code. Looking for skilled rails developers
who can help you meet your project
requirements? Contact us and hire Rails
developer.
Thank You
www.bacancytechnology.com

More Related Content

KEY
Ruby On Rails
PDF
Introduction to Ruby on Rails
PPT
Introduction to Ruby on Rails
PDF
O que há de novo no Rails 3
PDF
Security Goodness with Ruby on Rails
ODP
CodeIgniter PHP MVC Framework
PPT
Ruby On Rails Tutorial
PDF
RESTful Web Applications with Apache Sling
Ruby On Rails
Introduction to Ruby on Rails
Introduction to Ruby on Rails
O que há de novo no Rails 3
Security Goodness with Ruby on Rails
CodeIgniter PHP MVC Framework
Ruby On Rails Tutorial
RESTful Web Applications with Apache Sling

What's hot (20)

PDF
Distributed Ruby and Rails
PDF
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
PDF
Utiliser Webpack dans une application Symfony
PDF
Asp.Net Core MVC , Razor page , Entity Framework Core
PPT
Build Your Own CMS with Apache Sling
PDF
TorqueBox
PPT
Java presentation
PDF
Open Social Summit Korea
PDF
Developing Modern Java Web Applications with Java EE 7 and AngularJS
PDF
Cooking Perl with Chef: Real World Tutorial with Jitterbug
PPTX
Rails Engine Patterns
PPT
MVC Demystified: Essence of Ruby on Rails
PDF
REST APIs with Spring
PDF
Laravel Restful API and AngularJS
PDF
Why Laravel?
PPTX
Advance java Online Training in Hyderabad
PDF
Aligning Ember.js with Web Standards
PPTX
Java servlets and CGI
PDF
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
PDF
React Native in Production
Distributed Ruby and Rails
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Utiliser Webpack dans une application Symfony
Asp.Net Core MVC , Razor page , Entity Framework Core
Build Your Own CMS with Apache Sling
TorqueBox
Java presentation
Open Social Summit Korea
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Cooking Perl with Chef: Real World Tutorial with Jitterbug
Rails Engine Patterns
MVC Demystified: Essence of Ruby on Rails
REST APIs with Spring
Laravel Restful API and AngularJS
Why Laravel?
Advance java Online Training in Hyderabad
Aligning Ember.js with Web Standards
Java servlets and CGI
Symfony Live 2018 - Développez votre frontend avec ReactJS et Symfony Webpack...
React Native in Production
Ad

Similar to How to dockerize rails application compose and rails tutorial (20)

PDF
Rails Applications with Docker
PDF
Docker For Ruby On Rails : Meaning, Benefits, & Use Cases
PPTX
Ruby on Rails and Docker - Why should I care?
PDF
Rails in docker
PDF
Docker as development environment
PDF
Ruby microservices with Docker - Sergii Koba
PDF
Docker composeで開発環境をメンバに配布せよ
ODP
Ruby and Docker on Rails
PDF
Introducción a contenedores Docker
PPTX
Docker - A Ruby Introduction
PDF
Intro to Docker for (Rails) Developers
PPTX
Dockerizing Ruby Applications - The Best Practices
PDF
Docker for the Rubyist
PPTX
Real World Experience of Running Docker in Development and Production
PPTX
Migrating a large code-base to containers by Doug Johnson and Jonathan Lozins...
PDF
Docker for Ruby Developers
PPTX
Docker-Containerizing-Your-Applications(PPT-23).pptx
PDF
ContainerDayVietnam2016: Dockerize a small business
PDF
Introduction to Docker Compose
PDF
Troubleshooting Tips from a Docker Support Engineer
Rails Applications with Docker
Docker For Ruby On Rails : Meaning, Benefits, & Use Cases
Ruby on Rails and Docker - Why should I care?
Rails in docker
Docker as development environment
Ruby microservices with Docker - Sergii Koba
Docker composeで開発環境をメンバに配布せよ
Ruby and Docker on Rails
Introducción a contenedores Docker
Docker - A Ruby Introduction
Intro to Docker for (Rails) Developers
Dockerizing Ruby Applications - The Best Practices
Docker for the Rubyist
Real World Experience of Running Docker in Development and Production
Migrating a large code-base to containers by Doug Johnson and Jonathan Lozins...
Docker for Ruby Developers
Docker-Containerizing-Your-Applications(PPT-23).pptx
ContainerDayVietnam2016: Dockerize a small business
Introduction to Docker Compose
Troubleshooting Tips from a Docker Support Engineer
Ad

More from Katy Slemon (20)

PDF
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
PDF
Data Science Use Cases in Retail & Healthcare Industries.pdf
PDF
How Much Does It Cost To Hire Golang Developer.pdf
PDF
What’s New in Flutter 3.pdf
PDF
Why Use Ruby On Rails.pdf
PDF
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
PDF
How to Implement Middleware Pipeline in VueJS.pdf
PDF
How to Build Laravel Package Using Composer.pdf
PDF
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
PDF
How to Develop Slack Bot Using Golang.pdf
PDF
IoT Based Battery Management System in Electric Vehicles.pdf
PDF
Understanding Flexbox Layout in React Native.pdf
PDF
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
PDF
New Features in iOS 15 and Swift 5.5.pdf
PDF
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
PDF
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
PDF
Flutter Performance Tuning Best Practices From the Pros.pdf
PDF
Angular Universal How to Build Angular SEO Friendly App.pdf
PDF
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
PDF
Ruby On Rails Performance Tuning Guide.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
How Much Does It Cost To Hire Golang Developer.pdf
What’s New in Flutter 3.pdf
Why Use Ruby On Rails.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How to Implement Middleware Pipeline in VueJS.pdf
How to Build Laravel Package Using Composer.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
How to Develop Slack Bot Using Golang.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
Understanding Flexbox Layout in React Native.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
New Features in iOS 15 and Swift 5.5.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Ruby On Rails Performance Tuning Guide.pdf

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Machine learning based COVID-19 study performance prediction
PDF
Approach and Philosophy of On baking technology
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MIND Revenue Release Quarter 2 2025 Press Release
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Spectral efficient network and resource selection model in 5G networks
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)
Machine learning based COVID-19 study performance prediction
Approach and Philosophy of On baking technology
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development

How to dockerize rails application compose and rails tutorial

  • 1. HOW TO DOCKERIZE RAILS APPLICATION : COMPOSE AND RAILS TUTORIAL https://www.bacancytechnology.com/
  • 3. A curious developer and tech enthusiast never miss an opportunity to learn a little more every day! I can absolutely relate to this urge to learn. So, keeping your curiosity in mind, we are back with another Rails tutorial on how to dockerize rails applications with the help of Docker Compose. Hoping that you are familiar with what is Docker and why do we need Docker. Let’s get started with the app development and dockerizing it.
  • 5. Fire the following commands to create a rails app. mkdir ~/projects/noteapp cd ~/projects/noteapp
  • 7. Install Docker Community Edition Install Docker Compose As we are implementing Docker Compose, make sure about the following installations before getting started.
  • 9. The Dockerfile is the foundation of any Dockerized app. It contains all the instructions for building the application image. You can set this up by installing Ruby and all of its dependencies. The Dockerfile consists of the following instructions.
  • 10. FROM ruby:2.3.0 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /noteapp WORKDIR /noteapp ADD Gemfile /noteapp/Gemfile ADD Gemfile.lock /noteapp/Gemfile.lock RUN bundle install ADD . /noteapp // Dockerfile
  • 11. Dockerfile will keep the app code inside an image, building a container with Bundler, Ruby, and other dependencies. Therefore in the root directory of the application, create a new Dockerfile using the command touch Dockerfile and put the content of the above dockerfile inside it. Explanation FROM ruby:2.3.0: Tells Docker to use the prebuilt Ruby image. There are several choices, but this project uses the ruby:2.3.0 image. RUN: To run commands. Here, RUN is for installing different software pieces with Apt. WORKDIR: For stating the base directory from where all the commands are executed. ADD: For copying files from the host machine to our container.
  • 13. Next, open the editor and create a bootstrap Gemfile that loads Rails. source 'https://rubygems.org' gem 'rails', '~>5.0.0' Create an empty Gemfile.lock file to build our Dockerfile. touch Gemfile.lock // gemfile
  • 14. Dockerize Rails App: Add Portability, Modularity, and Scalability to your app Contact Bacancy and hire Rails developer to dockerize your rails application.
  • 16. Finally, moving towards the most important section. The docker-compose.yml file will consist of services needed for your app (web application and DB), for getting each other’s Docker image, and the config for connecting them and making it visible on the port.
  • 17. version: '2' services: db: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: noteapp MYSQL_USER: appuser MYSQL_PASSWORD: password // docker-compose.yml
  • 18. ports: - "3307:3306" app: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - ".:/noteapp" ports: - "3001:3000" depends_on - db links: - db
  • 20. Now build the skeleton of the rails application with the help of docker- compose run. docker-compose run app rails new . --force --database=mysql compose– builds the image for the app service, which we have to define inside our docker-compose.yml runs rails new – using that image it runs the app inside a new container database=mysql– to define the database Your application should be created after the command is successfully executed. List the files using ls -l
  • 22. In this section, we will connect the database as rails wants a database to be running on the localhost. We will also alter the database and username for aligning it with the defaults by the MySQL image. When we run the docker-compose command first, it will create a DB container that downloads the MySQL database image and creates a DB based on the environment variables set in the docker-compose.yml file. By now, we have a database container, and app structure created. We need to edit the config/database.yml file and set the configurations from the environment variables. Replace the contents of config/database.yml with the following:
  • 23. version: '2' services: db: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: noteapp MYSQL_USER: appuser MYSQL_PASSWORD: password ports: - "3307:3306" app: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' // docker-compose.yml
  • 24. volumes: - ".:/noteapp" ports: - "3001:3000" depends_on: - db links: - db environment : DB_USER: root DB_NAME: noteapp DB_PASSWORD: password DB_HOST: db After setting up the docker-compose.yml, run the docker-compose build command to build an image for the app and install all the required gems.
  • 25. Run the below command for database creation. docker-compose run --rm app rake db:migrate Before creating any migrations/models, let’s do a docker-compose up to start both app and database services and boot the application after making the changes in database.yml. We can see that rails is running on port 3000 in the container after the command is successfully executed. But, that’s not the port on the host, so we won’t be able to access it on the browser. As per docker- compose, we have exposed the port on the localhost from 3000 to 3001; therefore, it should be available on localhost:3001.
  • 26. Once you are done with the app running on the browser, create a model and perform the migration using these commands in a different console of the project directory. docker-compose run --rm app rails g scaffold note title body:text docker-compose run --rm app rake db:migrate Now, we can access the application on port 3001- localhost:3001/notes and perform actions on the application.
  • 28. mkdir ~/projects/noteapp cd ~/projects/noteapp Create Gemfile and empty Gemfile.lock (content is given above) Create Dockerfile (content is given above) Create docker-compose.yml (content is given above) docker-compose run app rails new . – force –database=mysql Make changes in config/database.yml docker-compose build docker-compose up http://localhost:3001 docker-compose run –rm app rails g scaffold note title body:text docker-compose run –rm app rake db:migrate http://localhost:3001/notes Watch the video tutorial on how to dockerize rails application as well.
  • 29. Source Code: dockerize- rails-app You can also clone the code and go through the project. Here’s the source code of the repository: dockerize-rails-app Commands: Stop, Restart, and Rebuild the Application To stop the application docker-compose down
  • 30. To restart the application docker-compose up To rebuild the application Rebuilding the application is a must when you’re trying different configs and altering the Gemfile or Compose file. ➡Sometimes only docker-compose up – build is enough. ➡But, if you want to rebuild the entire app fully, then use docker-compose run app bundle install, followed by docker-compose up –build for synchronizing changes between the Gemfile.lock and the host.
  • 32. That’s it for the tutorial: how to dockerize rails application using Docker compose. I hope the tutorial was helpful to you for building your own demo app and exploring more. Visit the Ruby on Rails tutorials page for similar tutorials, where you can explore your interests and play around with the code. Looking for skilled rails developers who can help you meet your project requirements? Contact us and hire Rails developer.