gpt4 book ai didi

ruby - 将Webservice打包到Docker镜像后不起作用

转载 作者:行者123 更新时间:2023-12-02 21:10:11 25 4
gpt4 key购买 nike

我用sinatra用ruby编写了一个简单的Web服务。如果我运行ruby app.rb,它将在localhost:4567上运行。我编写了一个Dockerfile来制作镜像并公开端口4567。

但是,当我运行docker image时,Web服务会运行,但是如果我尝试使用curl和浏览器连接到端口4567,它会显示Connection reset by peer

有人有什么建议吗?因为我不知道在这种情况下要检查什么。我已经尝试过一些模糊的东西,但是仍然..

Web服务通常在docker外部运行。

编辑1:

我已将图片推送到eivor/ruby。如果您运行它并转到浏览器进行检查,则会显示connection reset。是的,我在发布问题之前尝试了docker run -p 4567:4567 eivor/ruby

编辑2:这是app.rb

require 'sinatra'
require 'referal' # this is the gem that calculate reward points to users
require 'json'
require 'byebug'

get '/' do
'hello, world!'
end

# inside docker image, even get / returns connection reset by peer
# not to mention post data to it

post '/' do
data = JSON.parse(request.body.read)
input = []
data.each do | key, value |
input << value
end
invs, users = input.reduce([[],[]]) do | results, instruction |
results = classify(instruction, results[0], results[1])
results
end
res = export(users)
# byebug
puts res
end

post '/text' do
@data = request.body.readlines
#byebug
@processed = @data.map{ |s| process(s) }
@invs, @users = @processed.reduce([[],[]]) do | results, instruction |
results = classify(instruction, results[0], results[1])
results
end
@jsn = export(@users)
puts @jsn
end


这是Dockerfile,我从阿尔卑斯山构建了一个轻量级的 ruby
FROM alpine:3.5
ENV BUILD_PACKAGES bash curl-dev ruby-dev build-base git libstdc++ tzdata ca-certificates
ENV RUBY_PACKAGES ruby>2.3 ruby-irb ruby-rake ruby-io-console ruby-bigdecimal ruby-json

RUN apk update && apk upgrade
RUN apk add $BUILD_PACKAGES && apk add $RUBY_PACKAGES
RUN apk add ruby-bundler>1.17

RUN echo 'gem: --no-document' > /etc/gemrc && rm -rf /var/cach/apk/*

RUN gem install bundler

RUN mkdir /usr/app
WORKDIR /usr/app
RUN git init

COPY . /usr/app

RUN bundle install

RUN bundle exec rake install

EXPOSE 4567

CMD ["ruby", "./app.rb"]

如果我使用 ruby app.rbbundle exec rerun app.rb命令在docker外部运行,它将正常运行。但是使用docker image却没有。我运行命令:
docker run -p 4567:4567 eivor/ruby

服务器正在运行,
[2019-03-14 16:59:59] INFO  WEBrick 1.3.1
[2019-03-14 16:59:59] INFO ruby 2.3.8 (2018-10-18) [x86_64-linux-musl]
== Sinatra (v2.0.5) has taken the stage on 4567 for development with backup from WEBrick
[2019-03-14 16:59:59] INFO WEBrick::HTTPServer#start: pid=1 port=4567


但是当我尝试使用浏览器或curl访问时,它显示 connection reset by peer。如果我尝试使用curl进行发布,则实际上发送了数据,但它没有响应,而是挂断了我。
curl -v localhost:4567 --data-binary @test/input
* Rebuilt URL to: localhost:4567/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 4567 (#0)
> POST / HTTP/1.1
> Host: localhost:4567
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Length: 369
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 369 out of 369 bytes
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

最佳答案

发生此问题的原因是默认情况下(开发环境)Sinatra侦听127.0.0.1,在外部连接到容器的情况下,此侦听器不起作用

:bind - server hostname or IP address

String specifying the hostname or IP address of the interface to listen on when the :run setting is enabled. The default value in the development environment is 'localhost' which means the server is only available from the local machine. In other environments the default is '0.0.0.0', which causes the server to listen on all available interfaces.


因此,如果您继续在开发模式下运行,则需要将其更改为0.0.0.0,例如:
docker run -p 4567:4567 --name stack eivor/ruby bash -c 'ruby ./app.rb -o 0.0.0.0'
可以在Dockerfile中用作:
CMD ["ruby", "./app.rb", "-o", "0.0.0.0"]
或者,您可以在脚本中使用以下命令:
set :bind, '0.0.0.0'
然后从容器外部可以得到结果:
curl -v localhost:4567
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 4567 (#0)
> GET / HTTP/1.1
> Host: localhost:4567
> User-Agent: curl/7.64.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Content-Length: 13
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Server: WEBrick/1.3.1 (Ruby/2.3.8/2018-10-18)
< Date: Thu, 14 Mar 2019 17:17:20 GMT
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
hello, world!
有关更多配置,请检查以下内容: CONFIGURING SETTINGS

关于ruby - 将Webservice打包到Docker镜像后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55144337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com