I’m struggling with deploying my Rails app on Fly.io due to a port mismatch issue. My logs show:
Listening on http://0.0.0.0:3000
…
[PC01] instance refused connection. is your app listening on 0.0.0.0:8080? make sure it is not only listening on 127.0.0.1
Added PORT environment variable in fly.toml:
[env]
PORT = “8080”
Modified puma.rb to force port 8080 in production:
if ENV.fetch(“RAILS_ENV”, “development”) == “production”
port 8080
else
port ENV.fetch(“PORT”, 3000)
end
Tried changing the default port in puma.rb:
port ENV.fetch(“PORT”, 8080)
waiting for machine to be reachable on 0.0.0.0:8080 (waited 5.411656526s so far)
…
[PM05] failed to connect to machine: gave up after 15 attempts (in 8.418495488s)
…
[PR04] could not find a good candidate within 20 attempts at load balancing
Any ideas why my Rails app isn’t respecting the PORT settings or how to make Fly.io connect to the correct port?
It might be easier to change your fly.tom to specify internal_port = 3000; but you can specify the port when you launch bin/rails server -e production -p 4000. Just be sure that you either modify bin/docker-entrypoint if you are using sqlite3, or have a deploy release command defined in your fly.toml if you are running PostgreSQL.
As an alternative (if this is your first deploy), delete your Dockerfile and fly.toml (if you have one), and run:
bundle add dockerfile-rails --optimistic --group development
bin/rails generate dockerfile --force
This should inspect your source and create a consistent definition for you to use.
For some reason, that’s not the fly.toml that is being used. Per your log messages, your application is running on port 3000, but fly (based on looking at your fly.toml) is expecting the app to be running on port 8080.
Remove all references to port 8080 from your fly.toml and your source code, and run the following command to see what you will get: