Django with Docker app unable to connect to Fly postgres

I have a docker container containing a Django app, which works fine locally and is able to connect to a postgres DB in the container, however when I deploy to fly, it isn’t able to connect to the DB.

From other threads I understood that it was best if I launched the container with Fly postgres, which I did. However, when I run fly deploy I get the following error:

 ✖ release_command failed
-------
Error release_command failed running on machine e7840d0fd75778 with exit code 1.
Check its logs: here's the last 100 lines below, or run 'fly logs -i e7840d0fd75778':
  Pulling container image registry.fly.io/<app-name>:deployment-01HS1B6N6TMWBJW8PZWRZHA2GH
  Successfully prepared image registry.fly.io/<app-name>:deployment-01HS1B6N6TMWBJW8PZWRZHA2GH (565.967439ms)
  Configuring firecracker
  [    0.039130] PCI: Fatal: No config space access function found
   INFO Starting init (commit: 913ad9c)...
   INFO Preparing to run: `python manage.py migrate --noinput` as root
   INFO [fly api proxy] listening at /.fly/api
  2024/03/15 15:20:44 listening on [fdaa:0:9666:a7b:5bd4:e2bd:3f86:2]:22 (DNS: [fdaa::3]:53)
  Machine created and started in 1.529s
  Traceback (most recent call last):
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
      self.connect()
    File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
      return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in connect
      self.connection = self.get_new_connection(conn_params)
    File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
      return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
      connection = self.Database.connect(**conn_params)
    File "/usr/local/lib/python3.9/site-packages/psycopg/connection.py", line 738, in connect
      raise ex.with_traceback(None)
  psycopg.OperationalError: connection is bad: Name or service not known
  The above exception was the direct cause of the following exception:
  Traceback (most recent call last):
    File "/usr/app/greykite-env/manage.py", line 22, in <module>
      main()
    File "/usr/app/greykite-env/manage.py", line 18, in main
      execute_from_command_line(sys.argv)
    File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
      utility.execute()
    File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
      output = self.handle(*args, **options)
    File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 106, in wrapper
      res = handle_func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 117, in handle
      executor = MigrationExecutor(connection, self.migration_progress_callback)
    File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
      self.loader = MigrationLoader(self.connection)
    File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 58, in __init__
      self.build_graph()
    File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 235, in build_graph
      self.applied_migrations = recorder.applied_migrations()
    File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
      if self.has_table():
    File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 57, in has_table
      with self.connection.cursor() as cursor:
    File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
      return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 330, in cursor
      return self._cursor()
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 306, in _cursor
      self.ensure_connection()
    File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
      return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
      self.connect()
    File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
      self.connect()
    File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
      return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in connect
      self.connection = self.get_new_connection(conn_params)
    File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
      return func(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
      connection = self.Database.connect(**conn_params)
    File "/usr/local/lib/python3.9/site-packages/psycopg/connection.py", line 738, in connect
      raise ex.with_traceback(None)
  django.db.utils.OperationalError: connection is bad: Name or service not known
   INFO Main child exited normally with code: 1
   INFO Starting clean up.
   WARN hallpass exited, pid: 315, status: signal: 15 (SIGTERM)
  2024/03/15 15:20:49 listening on [fdaa:0:9666:a7b:5bd4:e2bd:3f86:2]:22 (DNS: [fdaa::3]:53)
  [    6.324202] reboot: Restarting system
  machine restart policy set to 'no', not restarting
-------
Checking DNS configuration for <app-name>.fly.dev
Error: release command failed - aborting deployment. error release_command machine e7840d0fd75778 exited with non-zero status of 1

My settings.py:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": config("POSTGRES_DATABASE"),
        "USER": config("POSTGRES_USER"),
        "PASSWORD": config("POSTGRES_PASSWORD"),
        "HOST": config("POSTGRES_HOST"),  # set in docker-compose.yml
        "PORT": 5432,  # default postgres port
    }
}

I have tried setting the secrets to the same values as I had locally, but this didn’t work. I have tried modifying the hostname, username and password to the values defined in the fly.io dashboard and by checking fly postgres connect -a <db-name> - I have even modified the password of the user to ensure it matches.

I’m really stuck, and I don’t know what to do to debug this…

Hi… Out of curiosity, what dashboard URL are you referring to?

(I didn’t see anything like this when I just checked my own https://fly.io/apps/<db-name>, but maybe it’s being rolled out gradually…)

I was on https://fly.io/apps/<db-name> and under Application Information → hostname there was a https://<db-name>.fly.dev, so I tried using that. It turned out that what I needed to use for the host was <db-name>.flycast.

The way I found it was by detaching the db from my application, and then re-attaching it. When I did that, the terminal printed out the db url, which I was able to use to find the db host name and the username and password, which I was then able to add one-by-one into my secrets list, and then it all came together.

I feel like this method isn’t ideal though, I kinda want to have more control or have a way to add the db secrets when I attach the db initially.

1 Like

Added dashboard

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