Docker and Rails are both well-known technologies in the web development sphere, each with unique and powerful benefits. These tools can compound their benefits if combined in the same application. Dockerizing a Ruby on Rails application enables new, innovative ways of delivering and running software.
The only downside is that it’s more difficult to install, so is it worth it?
Today, we’ll explore the benefits of each tool and how they work together to help you decide if they’re a good fit for your next project.
Here’s what we’ll cover today:
Combine the best of both tools to push your apps to the next level.
Docker is a set of tools used to package and run sandboxed environments known as containers. Like VMs, these containers are self-contained and isolated from the rest of the program, while also being more lightweight. Essentially, Docker allows you to package an application with all its dependencies into one standard unit.
The five main features of Docker are:
Packaging: The ability to package software into a reusable, shareable format known as images.
Distribution: The ability to easily share packaged software (images) with other people and deploy it to different machines.
Runtime: The ability to run, pause, restart or stop packaged software in a reliable way.
Infrastructure creation: Creating virtual machines ready to run Docker containers.
Orchestration and scaling: Managing the release of software to a single Docker node or across an entire cluster.
The main benefit of Docker containers is to package all the dependencies and resources a program needs to run in an easily distributable form.
Each container is its own environment, which allows you to simulate multi-node production scenarios on your own machine. These Docker containers are lightweight and are more efficient than VMs in certain situations.
Docker is also reliable and self-correcting. Whenever an instance dies, Docker automatically spawns new copies of your app to avoid a drop in production.
The self-correcting property of Docker is most useful in scaled solutions that distribute workload across multiple instances.
Docker interfaces with Rails well using pre-packaged, easily loadable versions of other technologies often used with Rails like Redis, MySQL, Postgres, or Node.
Ruby on Rails is a server-side, MVC model application framework used to develop structures for web applications. Rails prioritize quick application development with innovative timesaving functionalities like seamless table creation, table migrations, and scaffolding of views.
Rails is different from other frameworks because it is designed using the Convention over Configuration (CoC) paradigm. Rails achieves this through highly opinionated syntax, meaning there is a set “right way” to construct Rails apps. The CoC construction of Rails helps reduce the number of decisions developers need to make, speeding up the web app process overall.
The main features of Rails are:
Automated Testing: Rails provides built-in testing, so test cases are easier to write and execute.
Scaffolding: This allows you to easily create models that manage data, views that enable user-data interaction, and controllers that manage model-view communication.
Active Record: Rails operates on the assumption that you should ensure data access logic as part of the object to educate users on how to read and write to the database. Objects carry persistent data and behavior that operates on that data.
Rails Community: Rails has a very active community with regular product fixes and updates from the developers.
Convention over Configuration: Rails sets a correct way to construct apps that fit most use cases is more important than making configuration options for everything. The framework makes decisions for you to boost productivity.
You can combine the benefits of Docker and Rails by running your Rails applications within Docker containers.
From Rails, you retain the CoC opinionated environment and all the innovative tools for building applications fast. From Docker, you can apply self-correcting scaling and the ease of distribution with Docker’s sandboxed containers.
When you package Rails with Docker you get a portable and deterministic Rails development environment you can build from anywhere, anytime.
Look at how seamlessly we can combine Rails with Docker.
First, we create a Dockerfile that defines our Ruby app.
FROM ruby:2.7 RUN apt-get update -yqq RUN apt-get install -yqq --no-install-recommends nodejs COPY myapp /usr/src/app/ WORKDIR /usr/src/app RUN bundle install
Next, we can build our containers from any custom image, like
railsapp, using the terminal. By the end, we have a fully sandboxed instance of our rails app that we can distribute.
docker build -t railsapp . ＄ docker run -p 3000:3000 railsapp \ bin/rails s -b 0.0.0.0
You should use Docker with Rails if you’re comfortable with Rails and enjoy the speed it provides but also expect your app will be used by a large audience. Docker brings great scalability benefits with its dynamic container spinups, meaning your application can increase or decrease the number of instances in real-time.
You can even use containers with VMs to increase the density of applications using microservice architecture. For example, instead of running 3 services on 3 separate VMs, you could run 3 services in 3 containers on a single VM. Less VMs means it won’t be as expensive to maintain your infrastructure.
This is also a great combination of tools for teams. Without Docker, it would be a lot of work to standardize every developer’s operating system and versions of each tool. Using Docker, your application’s environment is within the container and automatically ensures each developer has a standardized environment.
Finally, Docker is ideal for Rails applications that have lots of dependencies and other required technologies. Connecting to external technologies like PostgreSQL or MS SQL can be a hassle on Rails.
Docker fixes this by allowing you to set dependencies and connections in a single file and package it to be loaded as one.
Docker Image: A file that contains a blueprint to construct a container to run in Docker. Essentially, it’s a list of what any container of that type should contain.
Docker Container: A runtime instance of an image. Each container consists of a Docker image, an execution environment, and a standard set of instructions.
Dockerfile: A file that contains all the commands to build an image that you would execute manually. This allows Docker to automatically create images.
Docker Compose: A built-in Docker tool used to define and run more complex Docker applications. Its main benefit is to be able to define multi-container applications in a single file, which can be launched with a single command.
Dockerize/Dockerized: The act of packaging an application using Docker. It’s often used to describe results after an application has been integrated with Docker.
Ruby Script: Commands in the Ruby programming language used to make application components dynamic.
Rails Server: A local webserver available on your browser at
http://localhost:3000. Used to test applications without a dedicated server.
Rails Gemfile A file used for describing file dependencies in a Ruby program. You can add these files to a container to easily add dependencies to your application.
Now that you have a better understanding of how these tools relate, it’s time to get hands-on. The best way to see the benefits of these tools is to use them together in your own project.
Some next topics to explore are:
To help you get started with your first project, Educative has created Docker for Rails Developers. This course walks you through Docker fundamentals, teaches you to integrate Rails with Docker, and teaches you to use the advanced Compose tool to transform your Rails app into a multiservice application.
By the end, you’ll have the hands-on experience you need to get the most out of both Docker and Rails.
Join a community of 1.4 million readers. Enjoy a FREE, weekly newsletter rounding up Educative's most popular learning resources, coding tips, and career advice.