How to Setup Ruby Docker Container using Docker Compose. If you are actively developing applications- Docker can simplify your workflow and your deployment. Docker is a great choice to keep things organized. This tutorial will show you how to set up a development environment for a Ruby on Rails application using Docker. You will create a containers for the application itself and we will use PostgreSQL database. Let’s start!
What is Ruby on Rails
Ruby is a free, open source and one of the popular programming languages developed by Yukihiro “Matz” Matsumoto in the mid-1990s. Compared to C and Java language, Ruby is a general purpose language. Rails makes web development easier, providing a pre built structure for development and everything you need to build a web app. Ruby on Rails uses the Model View Controller (MVC) architecture.
Ruby on Rails software code is built on top of Ruby. It is an open source web development framework that contains a collection of code libraries. It is considered a top choice of frameworks for startups as it is great for handling complex business logic and for delivering a web apps quickly. Ruby on Rails offers a complete solution for repetitive tasks like developing tables, forms or menus on the website.
Scaffolding where the programmer is able to define how the application database should function.
Customized library -vast libraries to assist developer with all the tools to produce high quality product (AJAX library, Database access library and Common tasks library).
Docker is a free and open source containerization platform that allows you to package up an application or service with all of its dependencies into a standardized image. It allows us to build and replicate images on any host, removing the inconsistencies of dev environments and reducing onboarding timelines considerably. The Docker image contains the code, runtime, system libraries and anything that required to deploy an application. If you are developing a Ruby application, you will need a specific version of Ruby, Bundler, project-specific gems and other dependencies based on the operating system. In this case, you can use Docker to keep things organized. Docker simplifies your workflow and the process of deploying your application to production.
Docker features
An open source solution
Easy and fast configuration.
An isolated, rapid framework.
Increases productivity.
Swarm. A clustering and scheduling tool for Docker containers.
Routing Mesh.
Cross cloud infrastructure.
Moderate CPU/memory overhead.
Fast reboot.
Security Management.
Follow this post to learn how to setup Ruby Docker container using Docker Compose.
How to Setup Ruby Docker Container using Docker Compose
Install Docker
To deploy a Ruby server using the Docker Compose, you will need to install the Docker on your server. By default, the latest version of Docker is not included in the Debian or Ubuntu default repository. So it is recommended to install Docker from the Docker’s official repository.
First, install all required dependencies using the following command:
By default, the latest version of Docker Compose is not included in the Ubuntu and Debian repository. So it is a good idea to download the latest version of Docker Compose from the GitHub.
Next, navigate to your rails directory and create a Rails container using the following command:
cd ~/rails/
docker run --rm -v ${PWD}:/usr/src -w /usr/src -ti ruby:alpine sh
This command will download and run the Ruby container, mount the local directory to the /usr/src directory, execute the shell command and move to the app directory:
[+] Running 1/0
⠿ Container rails-db-1 Recreated 0.1s
[+] Running 1/1
⠿ Container rails-db-1 Started 0.5s
Created database 'app_development'
Created database 'app_test'
You can verify the all Docker images using the following command:
docker images
You should see the following output:
REPOSITORY TAG IMAGE ID CREATED SIZE
rails_web latest c8c1f2c916ef 19 minutes ago 718MB
ruby alpine 0bf06f4f0410 4 weeks ago 68.3MB
postgres alpine ebf01b748a56 5 weeks ago 211MB
Now, run the following command to see all running applications:
docker-compose ps
You should see the following output:
NAME COMMAND SERVICE STATUS PORTS
rails-db-1 "docker-entrypoint.s…" db running 5432/tcp
rails-web-1 "bundle exec rails s…" web running 0.0.0.0:80->3000/tcp, :::80->3000/tcp
Access Ruby on Rails
Now, open your web browser and type the URL http://your-serevr-ip to access the Ruby on Rails application. You should see the Ruby on Rails dashboard on the following screen:
How to Setup Ruby Docker Container using Docker Compose Conclusion
In this post, we explained how to setup Ruby Docker container using Docker Compose. We also create a simple Rails application and deploy it inside the Docker container.
I really enjoy developing Rails applications with Docker Compose. It’s ensures your production environment is exactly the same as your local machine. As this technology matures even more this method of development will become more of a standard .
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
51vote
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.