Docker

How to Dockerize a Ruby Application with Sinatra?

In Docker, to boost the speed of development by minimizing the tasks, such as environment management, dockerized applications are used. By using this application, developers can save time, effort, and improve performance efficiently. They can dockerize any existing application like the Sinatra application.

The outcomes from this guide are:

How to Build Docker Image for Ruby Application with Sinatra?

To build a Ruby application with Sinatra, first, generate a Sinatra application file and paste the below-provided lines of code:

require 'sinatra'

get '/' do

'Hello! Its my first sinatra application'

end

After that, create a Gemfile that includes the Sinatra dependencies and add the following content to it:

source 'https://rubygems.org'

gem 'sinatra'

Next, add the Dockerfile and paste the provided code into it to create a Docker image that contains Ruby:

FROM ruby:2.7

WORKDIR /app

COPY Gemfile /app/

RUN bundle install

COPY . /app/

EXPOSE 4567

CMD ["ruby", "sample.rb", "-o", "0.0.0.0"]

Here:

  • FROM instruction is used for defining the base image. In our case, we have added the pre-built official image of Ruby from the Dockerfile.
  • WORKDIR instruction is utilized for specifying the working folder.
  • COPY is used for copying the Gemfile into the working directory.
  • RUN instructions are used for installing the bundle.
  • COPY instruction is utilized for copying into the working directory.
  • EXPOSE instructions are used to expose a port for the container.
  • CMD instruction is utilized for running a command when the container starts.

After that, built the new Docker image using the provided command:

docker build -t ruby-app1 .

Now, execute the provided command to check the newly created Docker image:

docker images

As you can see, the new image has been created successfully:

How to Dockerize a Ruby Application with Sinatra?

To dockerize a Ruby application with Sinatra, use the docker run command along with the container name, port number, and image name:

docker run -d --name=ruby_cont 4567 ruby-app1

To check whether the new container has been created or not by running the following command:

docker ps

Next, move to the Docker Desktop application, locate the newly created container, and click on it:

Lastly, open the Ruby application by using the localhost:52582 in the web browser’s URL bar and check it:

That’s all! We have compiled the procedure to dockerize a Ruby application with Sinatra.

Conclusion

To dockerize a Ruby application with Sinatra, first, create a Sinatra application file, Gemfile, and Dockerfile. Then, execute the docker build -t command to build an image. Next, run the container by using the docker run -d –name=<container-name> <port-number> <docker-image> command. This guide provided the easiest way to dockerize a Ruby application with Sinatra.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.