Running Ruby Sinatra inside a Docker container can't connect (via Mac host) or find commands (in another scenario)?

I tried two forms of Dockerfile to get a simple Ruby / Sinatra application, and it fails in both scenarios for various reasons (I will explain both points).

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

Application structure:

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

File Contents:

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 create a docker image this way:

docker build --rm -t ruby_app .

I start the container as follows:

docker run -d -p 7080:5000 ruby_app

Then I try to check if I can connect to a running service (on my Mac using Boot2Docker), for example:

curl $(boot2docker ip):7080

With version 1 of the Docker file, I get the following error before running 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 Docker file, it seems to work fine with the rack server inside the container, but I cannot connect through the host environment, so when I run the curl command, I get an error:

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

Does anyone know what I am missing? It doesn't seem to be so hard to get a very simple Ruby / Sinatra application running inside a Docker container from which I can access through my host (Mac OS X via Boot2Docker).

+5
source share
1 answer

Modify the docker file to use this instead:

 ["bundle", "exec", "rackup", "--host", "0.0.0.0", "-p", "5000"] 
+8
source

All Articles