在无法连接(通过Mac主机)或查找命令(在不同情况下)的Docker容器中运行Ruby Sinatra? [英] Running Ruby Sinatra inside a Docker container not able to connect (via Mac host) or find commands (in different scenario)?

查看:102
本文介绍了在无法连接(通过Mac主机)或查找命令(在不同情况下)的Docker容器中运行Ruby Sinatra?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了两种形式的Dockerfile来运行一个简单的Ruby / Sinatra应用程序,并且在这两种情况下,它均由于不同的原因而失败(我将在稍后解释)。

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).

有效地,我想从主机(使用Boot2Docker的Mac OS X)访问Sinatra Web服务器。

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

应用程序结构为:

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

文件内容为:

Dockerfile

版本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"]

版本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

我像这样构建docker镜像:

I build the docker image like so:

docker build --rm -t ruby​​_app。

我像这样运行容器:

docker run -d -p 7080:5000 ruby​​_app

然后尝试验证我是否可以连接到正在运行的服务(在Mac上使用Boot2Docker),如下所示:

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

curl $(boot2docker ip):7080

使用Dockerfile版本1,我得到以下错误之前能够运行curl命令:

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",'

使用版本2的Dockerfile,似乎可以从容器内部很好地运行机架服务器,但是我无法通过主机环境进行连接,因此在运行curl命令时,我会收到错误消息:

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:

卷曲:(7)无法连接到192.168.59.103端口7080:连接被拒绝

有人吗?对我想念的东西有什么想法吗?在Docker容器中运行一个非常简单的Ruby / Sinatra应用程序似乎并不难,我可以通过它通过主机(通过Boot2Docker在Mac OS X上)从中访问该容器。

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).

推荐答案

将dockerfile更改为使用该文件:

Change the dockerfile to use this instead:

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

这篇关于在无法连接(通过Mac主机)或查找命令(在不同情况下)的Docker容器中运行Ruby Sinatra?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆