The outcomes from this guide are:
- How to Build Docker Image for Ruby Application with Sinatra?
- How to Dockerize a Ruby Application with Sinatra?
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:
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:
gem 'sinatra'
Next, add the Dockerfile and paste the provided code into it to create a Docker image that contains Ruby:
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:
Now, execute the provided command to check the newly created Docker image:
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:
To check whether the new container has been created or not by running the following command:
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.