Docker Image with Postgres cannot connect to db

I’m really new to this and looking for a bit of guidance.
I’ve built a Vapor app with a Postgres Database and using Docker Compose to create an image running separate apps for the code and the database. This works as expected on my local machine.

However when running flyctl launch I am seeing error in the logs stating the app cannot connect to the database. Has anyone encountered a similar problem before?

This is the error from the logs:

2024-06-25T16:52:43.698 app[3d8d172aeed3e8] scl [info] 2024-06-25T16:52:43+0000 error codes.vapor.application : database-id=psql [AsyncKit] Opening new connection for pool failed: PSQLError(code: connectionError, underlying: NIOPosix.NIOConnectionError(host: "localhost", port: 5432, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111)), NIOPosix.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 111))]))

Hi @dannybravo!

It’s not quite that easy to deploy docker-compose stuff on Fly.io — you generally have to break things up into their individual components/containers.

From the error your posted, it looks like your app is trying to connect to the DB on localhost — this would be fine on your local Machine but on Fly, that would mean that the database process is running within the same Fly Machine as the app. I doubt that is the case here?

Why not rather try spinning up a new Fly Postgres or Supabase database alongside your app? The Vapor docs actually have a guide especially for that, which includes details on how to set up your database: Vapor: Deploy → Fly

Hope this helps!

1 Like

Thank you, this might just be the pointer I needed!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.