How to Dockerize your Ruby-On-Rails Application?

MSys Editorial Jul 16 - 4 min read

Audio : Listen to This Blog.


Packaging an application along with all of its bin/lib files, dependencies and deploying it in complex environments is much more tedious than it sounds. In order to extenuate it, Docker, an open-source platform, enables applications to quickly group their components and eliminates the friction between development, QA, and production environments. Docker is a lightweight packaging solution that can be used instead of a virtual machine. Docker is an open-source engine to create portable, lightweight containers from any application.

Docker is hardware- and platform-agnostic, which means a Docker container can run on any supported hardware or operating system. The fact that it takes less than a second to spawn a container from a Docker image justifies that Docker really is lightweight as compared to any other virtualization mechanism. Also the Docker images are less than a tenth the size of their counterpart virtual machine images. The images created by extending a Docker base image can be as tiny as few megabytes. This makes it easier and faster to move your images across different environments.
Docker Hub is the central repository for Docker. Docker Hub stores all the public as well as private images. Private images are only accessible for a given users account or team to which it belongs. Docker Hub can be linked to Github or Bitbucket to trigger auto builds. The result of such a build is ready to deploy the application’s Docker image.

Docker provides mechanism to separate application dependencies, code, configuration, and data by providing features such as container linking, data volumes, and port mapping. Dependencies and configuration is specified in the Dockerfile script. The Dockerfile installs all the dependencies, pulls the application code from the local or remote repository, and builds a ready-to-deploy application image.

Container Linking

Docker container linking mechanism allows communication between containers without exposing the communication ports and details. The below command spawns a Tomcat application container and links it to the mysql-db-container. The Tomcat application can communicate to the mysql-db by using the environment variables (like db:host, db:port, db:password) exposed by mysql-db-container there by providing maximum application security.
docker run –link mysql:mysql-db-container clogeny/tomcat-application

Data Volumes

Docker provides data volumes to store, backup, and separate the application data from the application. Data volumes can be shared between multiple containers and read write policies can be specified for a given data volume. Multiple data volumes can be attached to the container using the flag -v multiple times. Docker also allows mounting a host directory as data volume to a container.
docker run -v /dbdata –name mysql-instance1 my-sql
#this creates dbdata volume inside the mysql-instance1 container
docker run –volumes-from mysql-instance1 –name my-sql-instance2 my-sql-server
#mounts and share all the volumes from mysql-instance1container

Dockerizing a Ruby on Rails Application

4 Simple steps to Dockerize your ruby-on-rails application

  1. Install Docker
  2. Create a Dockerfile as below in your application directory.
    FROM rails # use the rails image from the Docker Hub central repository
    MAINTAINER Clogeny <[email protected]>
    ADD ./src ./railsapp #Copies the source files from host to the container. URL to the code repository can also be used
    RUN bundle install
    EXPOSE 3000 #Expose port 3000 to communicate with the RoR server
    ENTRYPOINT rails s # run the RoR server with “rail s” command
  3. Build the application image. This command creates a ready-to-run rails image with your rails application deployed.
    docker build -t clogeny/my-RoR-app # -t specifies the name of the image which gets created
  4. Push the application to central repository so that the QA can use it to test the application. The image can be used to speed up and revolutionize the CI/CD workflow.
    docker push clogeny/my-RoR-app # Upload the Docker image to the central repo

Deploying the Dockerized Application

Deployment requires executing just one command to get the application up and running on the test machine. Assuming Docker is installed on the host, all we need to do is execute the “docker run” command to spawn a Docker container.
docker run # Spawn a docker container
-t # -t flag is used to show the stdOut and stdErr on the commandLine
-p 3000:3010 # -p flag is used to map container port 3000 to the host port 3010
clogeny/my-RoR-app # Use the “my-RoR-app” image earlier uploaded to the repo.
And here we are, the Docker container is up and running in a matter of a few seconds. We can log into the application using the URL http://localhost:3010

Leave a Reply

MSys rescued one of our biggest clients by conflating DevOps and Containerization, powered by Automation. The application downtime, prevalent during the release cycle slumbered down by 100 percent. To learn the whole story download our success story on “MSys DevOps and Containerization Solutions Help Fast-Food Giant to Achieve Zero Downtime Deployment”