heroku 容器内的 Puma 服务器无法连接到端口

Puma server inside heroku container fails to connect to port

我有一个docker容器:(名为Dockerfile.web,相关部分)

ARG PORT=3000
ENV PORT=$PORT

EXPOSE $PORT

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["rails", "server"]

美洲狮:config/puma.rb

before_fork do
  ActiveRecord::Base.connection_pool.disconnect! if defined? ActiveRecord
end

on_worker_boot do
  ActiveRecord::Base.establish_connection if defined? ActiveRecord
end

preload_app!

所以当我 运行 heroku container:push web 后跟 heroku container:release web 时,应用程序部署了,但是 puma 服务器没有启动 b/c 端口无法连接到:

Booting Puma
Rails 5.2.2 application starting in development 
Run `rails server -h` for more startup options
 Puma starting in cluster mode...
 * Version 3.12.0 (ruby 2.6.0-p0), codename: Llamas in Pajamas
 * Min threads: 5, max threads: 5
 * Environment: development
 * Process workers: 1
 * Preloading application
 * Listening on tcp://localhost:41626
 Use Ctrl-C to stop
 - Worker 0 (pid: 28) booted, phase: 0
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Stopping process with SIGKILL

我正在关注 the official guide 但我还没有添加以下部分:

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

我的错误可能在哪里?有什么想法吗?

我不确定你为什么遗漏了这部分:

port        ENV['PORT']     || 3000

您不能选择自己的端口。 Use the $PORT that Heroku gives you.

对于到达这里的人,解决方案是直接从 docker CMD 调用 puma 服务器,以便应用配置文件中的所有设置。原因是在 heroku 上,像端口这样的东西是动态添加的,所以容器镜像(之前构建的)不会有这些镜像。不知何故,在 Dockerfile 中使用 $PORT 值也无济于事。解决方案是

CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

# or if you prefer the bash style
CMD bundle exec puma -C config/puma.rb