Docker Rails 应用程序无法提供服务 - curl: (56) Recv failure: Connection reset by peer
Docker Rails app fails to be served - curl: (56) Recv failure: Connection reset by peer
我使用以下 Dockerfile 构建了一个 Rails 应用程序容器:
$ cat Dockerfile
FROM ruby:2.2
MAINTAINER Luca G. Soave <luca.soave@gmail.com>
RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN bundle install
EXPOSE 3000
CMD ["rails", "server"]
构建成功:
$ docker build -t querier .
Sending build context to Docker daemon 46.75 MB
Sending build context to Docker daemon
...
Step 10 : CMD rails server
---> Running in 8eb62f8a579a
---> 65eee929d518
Removing intermediate container 8eb62f8a579a
Successfully built 65eee929d518
容器正在运行:
$ docker logs -f 89ff3bb8c584
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-01-06 19:56:56] INFO WEBrick 1.3.1
[2015-01-06 19:56:56] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux]
[2015-01-06 19:56:56] INFO WEBrick::HTTPServer#start: pid=1 port=3000
它正在导出 3000/tcp 端口:
$ docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89ff3bb8c584 querier:latest "rails server" 16 minutes ago Up 16 minutes 0.0.0.0:3000->3000/tcp gloomy_babbage
但服务器无法访问 'internally' 或 'externally' Docker:
$ curl 0.0.0.0:3000
curl: (56) Recv failure: Connection reset by peer
$ curl 172.17.0.24:3000
curl: (7) Failed to connect to 172.17.0.24 port 3000: Connection refused
但似乎一切正常:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 89ff3bb8c584
172.17.0.24
$ netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 10.36.32.1 0.0.0.0 UG 0 0 0 wlan0
10.0.3.0 * 255.255.255.0 U 0 0 0 lxcbr0
10.36.32.0 * 255.255.248.0 U 0 0 0 wlan0
172.17.0.0 * 255.255.0.0 U 0 0 0 docker0
$ ping 0.0.0.0
PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.069 ms
^C
--- 0.0.0.0 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.061/0.066/0.069/0.007 ms
$ ping 172.17.0.24
PING 172.17.0.24 (172.17.0.24) 56(84) bytes of data.
64 bytes from 172.17.0.24: icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from 172.17.0.24: icmp_seq=2 ttl=64 time=0.107 ms
64 bytes from 172.17.0.24: icmp_seq=3 ttl=64 time=0.076 ms
^C
我也在浏览器中尝试过,但没有。主机系统是:
$ docker -v
Docker version 1.4.1, build 5bc2ff8
$ uname -a
Linux basenode 3.11.0-18-generic #32-Ubuntu SMP Tue Feb 18 21:11:14 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
有什么想法吗?
Rails server documentation 声明服务器默认绑定到 localhost
,这通常会阻止 dockerized 应用程序接受连接。尝试将其更改为 0.0.0.0
.
我刚刚标记了 Mykola Gurov 的回答,因为这是我问题的正确 'cause',
无论如何,我还想添加我实施的解决方案来解决这个问题,只是为了跟踪 pourpose。
我通过添加默认选项修改了 config/boot.rb:
$ cat config/boot.rb
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
######## added lines ########
require 'rails/commands/server'
module Rails
class Server
alias :default_options_alias :default_options
def default_options
default_options_alias.merge!(:Host => '0.0.0.0')
end
end
end
###############################
如果您不想修改 config/boot.rb,另一种解决方案可能是强制 Dockerfile 将 0.0.0.0 主机绑定为 ENTRYPOINT/CMD 参数:
$ cat Dockerfile
FROM ruby:2.2
RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN bundle install
EXPOSE 3000
ENTRYPOINT ["rails", "server", "-b", "0.0.0.0"]
我使用以下 Dockerfile 构建了一个 Rails 应用程序容器:
$ cat Dockerfile
FROM ruby:2.2
MAINTAINER Luca G. Soave <luca.soave@gmail.com>
RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN bundle install
EXPOSE 3000
CMD ["rails", "server"]
构建成功:
$ docker build -t querier .
Sending build context to Docker daemon 46.75 MB
Sending build context to Docker daemon
...
Step 10 : CMD rails server
---> Running in 8eb62f8a579a
---> 65eee929d518
Removing intermediate container 8eb62f8a579a
Successfully built 65eee929d518
容器正在运行:
$ docker logs -f 89ff3bb8c584
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-01-06 19:56:56] INFO WEBrick 1.3.1
[2015-01-06 19:56:56] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux]
[2015-01-06 19:56:56] INFO WEBrick::HTTPServer#start: pid=1 port=3000
它正在导出 3000/tcp 端口:
$ docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89ff3bb8c584 querier:latest "rails server" 16 minutes ago Up 16 minutes 0.0.0.0:3000->3000/tcp gloomy_babbage
但服务器无法访问 'internally' 或 'externally' Docker:
$ curl 0.0.0.0:3000
curl: (56) Recv failure: Connection reset by peer
$ curl 172.17.0.24:3000
curl: (7) Failed to connect to 172.17.0.24 port 3000: Connection refused
但似乎一切正常:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 89ff3bb8c584
172.17.0.24
$ netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 10.36.32.1 0.0.0.0 UG 0 0 0 wlan0
10.0.3.0 * 255.255.255.0 U 0 0 0 lxcbr0
10.36.32.0 * 255.255.248.0 U 0 0 0 wlan0
172.17.0.0 * 255.255.0.0 U 0 0 0 docker0
$ ping 0.0.0.0
PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.069 ms
^C
--- 0.0.0.0 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.061/0.066/0.069/0.007 ms
$ ping 172.17.0.24
PING 172.17.0.24 (172.17.0.24) 56(84) bytes of data.
64 bytes from 172.17.0.24: icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from 172.17.0.24: icmp_seq=2 ttl=64 time=0.107 ms
64 bytes from 172.17.0.24: icmp_seq=3 ttl=64 time=0.076 ms
^C
我也在浏览器中尝试过,但没有。主机系统是:
$ docker -v
Docker version 1.4.1, build 5bc2ff8
$ uname -a
Linux basenode 3.11.0-18-generic #32-Ubuntu SMP Tue Feb 18 21:11:14 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
有什么想法吗?
Rails server documentation 声明服务器默认绑定到 localhost
,这通常会阻止 dockerized 应用程序接受连接。尝试将其更改为 0.0.0.0
.
我刚刚标记了 Mykola Gurov 的回答,因为这是我问题的正确 'cause', 无论如何,我还想添加我实施的解决方案来解决这个问题,只是为了跟踪 pourpose。
我通过添加默认选项修改了 config/boot.rb:
$ cat config/boot.rb
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
######## added lines ########
require 'rails/commands/server'
module Rails
class Server
alias :default_options_alias :default_options
def default_options
default_options_alias.merge!(:Host => '0.0.0.0')
end
end
end
###############################
如果您不想修改 config/boot.rb,另一种解决方案可能是强制 Dockerfile 将 0.0.0.0 主机绑定为 ENTRYPOINT/CMD 参数:
$ cat Dockerfile
FROM ruby:2.2
RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN bundle install
EXPOSE 3000
ENTRYPOINT ["rails", "server", "-b", "0.0.0.0"]