Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Ruby Sinatra inside a Docker container not able to connect (via Mac host) or find commands (in different scenario)?

I've tried two forms of Dockerfile to get a simple Ruby/Sinatra app running, and in both scenarios it fails for different reasons (I'll explain both in a moment).

Effectively I want to access the Sinatra web server from my host (Mac OS X using Boot2Docker).

The app structure is:

.
├── Dockerfile
├── Gemfile
├── app.rb
├── config.ru

The contents of the files are:

Dockerfile

Version 1...

FROM ruby
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile /app/
RUN bundle install --quiet
COPY . /app
EXPOSE 5000
ENTRYPOINT ["bash"]
CMD ["bundle", "exec", "rackup", "-p", "5000"]

Version 2...

FROM ubuntu:latest

RUN apt-get -qq update
RUN apt-get -qqy install ruby ruby-dev
RUN apt-get -qqy install libreadline-dev libssl-dev zlib1g-dev build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
RUN gem install bundler

RUN mkdir -p /app
WORKDIR /app
COPY Gemfile /app/
RUN bundle install --quiet
COPY . /app
EXPOSE 5000
CMD ["bundle", "exec", "rackup", "-p", "5000"]

Gemfile

source "https://rubygems.org/"

gem "puma"
gem "sinatra"

app.rb

require "sinatra/base"

class App < Sinatra::Base
  set :bind, "0.0.0.0"

  get "/" do
    "<p>hello world</p>"
  end
end

config.ru

require "sinatra"
require "./app.rb"

run App

I build the docker image like so:

docker build --rm -t ruby_app .

I run the container like so:

docker run -d -p 7080:5000 ruby_app

I then try to verify I can connect to the running service (on my Mac using Boot2Docker) like so:

curl $(boot2docker ip):7080

With version 1 of the Dockerfile I get the following error before being able to run the curl command:

/usr/local/bundle/bin/rackup: line 9: require: command not found
/usr/local/bundle/bin/rackup: rackup: line 10: syntax error near unexpected token `('
/usr/local/bundle/bin/rackup: rackup: line 10: `ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../../../../app/Gemfile",'

With version 2 of the Dockerfile it seems to run the rack server fine from inside the container but I'm unable to connect via the host environment and so when running the curl command I get the error:

curl: (7) Failed to connect to 192.168.59.103 port 7080: Connection refused

Does anyone have any idea as to what I'm missing? Seems it shouldn't be this hard to get a very simple Ruby/Sinatra app running inside a Docker container from which I can access via my host (Mac OS X via Boot2Docker).

like image 480
Integralist Avatar asked May 04 '15 09:05

Integralist


1 Answers

Change the dockerfile to use this instead:

["bundle", "exec", "rackup", "--host", "0.0.0.0", "-p", "5000"]
like image 129
Anthony Avatar answered Nov 02 '22 04:11

Anthony