title: Running Ruby Sinatra inside a Docker container not able to connect (via Mac host) or find commands (in different scenario)?
body: <p>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).</p>
  
  <p>Effectively I want to access the Sinatra web server from my host (Mac OS X using Boot2Docker).</p>
  
  <p>The app structure is:</p>
  
  <pre><code>.
  ├── Dockerfile
  ├── Gemfile
  ├── app.rb
  ├── config.ru
  </code></pre>
  
  <p>The contents of the files are:</p>
  
  <p><strong>Dockerfile</strong></p>
  
  <p>Version 1...</p>
  
  <pre><code>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"]
  </code></pre>
  
  <p>Version 2...</p>
  
  <pre><code>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"]
  </code></pre>
  
  <p><strong>Gemfile</strong></p>
  
  <pre><code>source "https://rubygems.org/"
  
  gem "puma"
  gem "sinatra"
  </code></pre>
  
  <p><strong>app.rb</strong></p>
  
  <pre><code>require "sinatra/base"
  
  class App &lt; Sinatra::Base
    set :bind, "0.0.0.0"
  
    get "/" do
      "&lt;p&gt;hello world&lt;/p&gt;"
    end
  end
  </code></pre>
  
  <p><strong>config.ru</strong></p>
  
  <pre><code>require "sinatra"
  require "./app.rb"
  
  run App
  </code></pre>
  
  <p>I build the docker image like so:</p>
  
  <p><code>docker build --rm -t ruby_app .</code></p>
  
  <p>I run the container like so:</p>
  
  <p><code>docker run -d -p 7080:5000 ruby_app</code></p>
  
  <p>I then try to verify I can connect to the running service (on my Mac using Boot2Docker) like so:</p>
  
  <p><code>curl $(boot2docker ip):7080</code></p>
  
  <p>With version 1 of the Dockerfile I get the following error <em>before</em> being able to run the curl command:</p>
  
  <pre><code>/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",'
  </code></pre>
  
  <p>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:</p>
  
  <p><code>curl: (7) Failed to connect to 192.168.59.103 port 7080: Connection refused</code></p>
  
  <p>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).</p>