Ruby applications in Docker

How to setup Docker container to run Ruby Web Application using Sinatra.

Here is quick instructions how to start quickly.

Install Docker

It’s very easy, just go to Docker site and follow instructions for your system.

Create Gemfile for project

source 'https://rubygems.org'

gem 'sinatra'
gem 'rack'
gem 'rack-test'
gem 'json'

Create simple Web Application

Let’s create our application start file and put it into app folder. We’ll use Sinatra as server.

require 'sinatra'

set :bind, '0.0.0.0'

get '/' do
  "Hello, Sinatra"
end

For more details how to use Sinatra, use project’s page.

Create Dockerfile

FROM ruby:2.2.2

RUN apt-get update && apt-get install -y net-tools

ENV APP_HOME /app
ENV HOME /root
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

ADD . .

RUN bundle install

ENV PORT 3000
EXPOSE 3000
CMD ["ruby", "app/app.rb"]

We use ruby base image of version 2.2.2. Then we update apt packages repository and install net-tools.

Setting up our application home path and working directory.

Final structure of out work project directory:

$ ls -R
Dockerfile Gemfile    app

./app:
app.rb

Build Docker container

Build container:

$ docker build -t web_app .

and run it:

$ docker run --rm -itP $(pwd):/app -p 3000:3000 web_app

Should output something like this:

[2017-05-19 12:19:49] INFO  WEBrick 1.3.1
[2017-05-19 12:19:49] INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
== Sinatra (v2.0.0) has taken the stage on 3000 for development with backup from WEBrick
[2017-05-19 12:19:49] INFO  WEBrick::HTTPServer#start: pid=1 port=3000

Check it

$ curl http://localhost:3000/
Hello, Sinatra

Links


Опубликовано: Май 19, 2017 ~ Ruby, Docker