How I used Docker with Rails

In this article you’ll be getting an idea regarding Docker and how I took the advantage of docker for my Ruby on Rails applications. To explain this I will be creating a simple project with RoR with the database of MySQL.

What is docker actually?

Simply…. Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Don’t think too complex. Container is a space where we can use to bundle up an application with all dependencies.

What is the use of containers?

Let’s say like this. Assume you have to migrate to a new computer in the middle of the development. You all know that rebuilding the development machine is a real headache.

But :)

If you use containers to build up the application with the help of Docker, we don’t have to worry about that as developers. The application will run on any other Linux machine regardless of any customized settings.

Okay, That’s all about the Docker. If you need more about this, go to the following link.

Download and Install Docker

If you have installed docker don’t forget to create an account in DokcerHub too. here is the link for that.

You can push your docker built images here, just like GIT

Let’s start the with the rails application. Create you app folder. Go there and create a Dockerfile.

My App folder

/home/admat/Workspace/Rahasak/labs/rahasak-octopus/registrar-web

Creating the Dockerfile

touch Dockerfile

Editing the Dockerfile

nano Dockerfile

Dockerfile is a text file we use to write commands in order to create our docker image. Just like setting up your development environment. Without installing them one by one in the terminal we are about to write Dockerfile.

This is my Dockerfile,

Here, From means the initial Image we use, Those images are available in the Docker Hub.

For this, We need the sample Gem files. So create both Gemfile and Gemfile.lock. ( You know these if you are a Rails Developer ;) )

Create Gemfile

touch Gemfile

Open Gemfile

nano Gemfile

Add initial Gem

source 'https://rubygems.org'ruby '2.6.0'
gem 'rails', '~> 5.2.2'

Save it with Ctrl + X → Y for yes ( If you are using the Nano editor)

Create the Gemgfile.lock

touch Gemfile.lock

Entrypoint

This script will be executed every time the container gets started. We can put the rails specific scripts to this, Like Rake DB commands and Clear Server.pid.

Here is my docker-entrypoint.sh

Generate the Rails Project now

docker run --name rails new . --force --no-deps --database=mysql

It will create the files for the rails application like normal

Now, you can do the database initialization as usual. I am using ATOM as my editor.

atom .

This opens my code base in the ATOM editor. So i can change my config about the database. ( Here I have used MySQL )

here is my database.yml

default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOST'] %>
development:
<<: *default
test:
<<: *default
production:
<<: *default

You can assign all password and credentials in a .env file. If you do, don’t forget to include it in .gitignore and .dockerignore

here is my ENV

# Rails Application
DB_NAME=octopus_app_db
DB_USER=root
DB_PASSWORD=password
DB_HOST=db
# MySQL
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=octopus_app_db
MYSQL_USER=appuser
MYSQL_PASSWORD=password

For this mysql you run it using docker too. Here is the link to the mysql image

just use the following command to pull the image

docker pull mysql:5.7

to start the Mysql

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7

Build the Image

docker build -t admat/registrar-web .

name of the image: admat/registrar-web

. means the all in this folder

check the image in the terminal like this

docker images
Here is my image

RUN the rails application

docker run -e DB_NAME=registrar_app_db \                                                                                    
-e DB_USER=root \
-e DB_PASSWORD=password \
-e DB_HOST=172.21.0.2 \
-p 3001:3000 admat/registrar-web

I have to set the environment variable in order to add them in the container use.

That’s all to run the application. if you go to localhost:3001 your app should be there.

Next..

Creating the Docker compose file

We can sue the docker compose file to implement multiple docker images and run them in one command.

here is my docker-compose.yml file

you can have this in the same folder for now and put

build: .

Best practice is to put this is a different folder and and the image file instead of build

image: admat/registrar-web

Build Docker compose file

docker-compose build

this will run the Dockerfile we defined and build the image

Run the Docker compose

docker-compose up

this command will up all the services mentioned, if you wanna up one service by one,

docker-compose up -d [service-name]

When i run my docker-compose

All those running containers can be seen by

docker ps -a

To see only the running containers just use

docker ps

To access the Rails console we can go inside the docker machine. For that we can use the bash command

docker exec -it [container-name] bash

if success you must be inside the machine, you can run

rails c
rake commands
etc..

finally

You can push your image in to the DockerHub,

From this stage you must have a dockerhub account with a unique username.

docker login

and then if authenticated you can use push command like

docker push [image-name]:tagname

docker push admat/registar-web:latest

So this is how I configured docker with rails and made my development and deployments easy.

Start yours today.

Happy coding :)

Cheers.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store