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.

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.

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.

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

Editing the 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

Open Gemfile

Add initial Gem

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

Create the Gemgfile.lock

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

Here is my

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.

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

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

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

to start the Mysql

name of the image: admat/registrar-web

. means the all in this folder

check the image in the terminal like this

Here is my image

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.

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

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

this will run the Dockerfile we defined and build the image

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

When i run my docker-compose

All those running containers can be seen by

To see only the running containers just use

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

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

You can push your image in to the DockerHub,

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

and then if authenticated you can use push command like

docker push [image-name]:tagname

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

Start yours today.

Happy coding :)


Senior Software Engineer ❤️ Node | Ruby