Connecting to Postgres (.internal) from Dockerfile on deploy

Hello,

I am having issues connecting to a Postgres application through a Dockerfile on deploy. The database is available when connecting from directly from the application itself (i.e fly ssh console).

P1001: Can't reach database server at `postgres.internal`:`5432`                                                        
Please make sure your database server is running at `postgres.internal`:`5432`.                                         

Any help on the matter would be greatly appreciated.

Best regards,
Alex

Hi Alex,

Just to confirm, have you been through this:
https://fly.io/docs/getting-started/multi-region-databases/#configure-connection-strings

Also from this page:

Note that for the app to be able to attach to the database it must have private_network enabled in fly.toml.

If you’ve already reviewed these, please paste the fly.toml and the dockerfile here.

Thanks.

1 Like

Hi FrequentFlyer,

Private network is enabled:

app = "backend"

kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]
  LOG_LEVEL = "debug"
  PORT = 8080

[experimental]
  allowed_public_ports = []
  auto_rollback = true
  private_network = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []

  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"
...
ENV NODE_ENV=production
ENV DATABASE_URL=postgres://backend:[PASSWORD]@top2.nearest.of.postgres.internal:5432/backend
COPY . .
RUN yarn prisma generate
RUN yarn build
RUN yarn prisma migrate status 
EXPOSE 8080
USER node
ENTRYPOINT ["node", "dist"]

Best regards,
Alex

What is the name of your database application? It’s probably not postgres. If you app name is my-postgres-cluster then your DATABASE_URL should also contain it, like:

postgres://backend:[PASSWORD]@top2.nearest.of.my-postgres-cluster.internal:5432/backend

One thing: setting the DATABASE_URL in the Dockerfile is insecure as it will show up when inspecting images.

Instead, you can run yarn prisma migrate as a release command instead of requiring your build to access the database. Release commands run after build, but before deployment, and they inherit the app environment/secrets. Builds do not.

See App Configuration (fly.toml)

Also: private networks are enabled by default, so you no longer need to add this to your fly.toml explicitly.

1 Like