Docker Community Forums

Share and learn in the Docker community.

Postgres connect keeps being reset

I have a Django app and Postgres DB running on two separate containers. The DB connection from Django continues to be set whenever I startup the containers. I tried restarting Django, but get the same result. I’m able to go into psql from the Django container and view the tables.


  Kernel: 5.11.0-36-generic x86_64 bits: 64 compiler: N/A 
  Desktop: Cinnamon 4.6.7 wm: muffin 4.6.3 dm: LightDM 1.30.0 
  Distro: Linux Mint 20 Ulyana base: Ubuntu 20.04 focal

Docker version 20.10.8, build 3967b7d

docker-compose version 1.25.0, build unknown


traceback (most recent call last):
  File "/usr/local/lib/python3.9/", line 973, in _bootstrap_inner
  File "/usr/local/lib/python3.9/", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/utils/", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/channels/management/commands/", line 76, in inner_run
  File "/usr/local/lib/python3.9/site-packages/django/core/management/", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/", line 53, in __init__
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/", line 77, in applied_migrations
    if self.has_table():
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/usr/local/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/", line 259, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/", line 235, in _cursor
  File "/usr/local/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/", line 219, in ensure_connection
  File "/usr/local/lib/python3.9/site-packages/django/db/", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/", line 219, in ensure_connection
  File "/usr/local/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

Watching for file changes with StatReloader
Performing system checks...

Docker Compose

    build: .
      - "8000:80"
      - db
      - rasa_server
      - rasa_actions_server
      - redis
      - "host.docker.internal:host-gateway" # for to work on Linus
      - type: bind
        source: .
        target: /code
    command: [ "python", "", "runserver" ,"" ]
    build: ./databasedata/
      - "8003:5432"
      - type: volume
        source: db
        target: /var/lib/postgresql/data

root@ddf41bff0bb4:/code# psql -h host.docker.internal -p 8003 -U postgres

Docker volume (DB)

docker volume inspect ava_db
        "CreatedAt": "2021-09-26T03:14:56-04:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "ava",
            "com.docker.compose.version": "1.25.0",
            "com.docker.compose.volume": "db"
        "Mountpoint": "/var/lib/docker/volumes/ava_db/_data",
        "Name": "ava_db",
        "Options": null,
        "Scope": "local"

Postgres container logs

pg_restore: creating FK CONSTRAINT "public.newweb_product_tags newweb_product_tags_tag_id_f6ff674f_fk_newweb_tag_id"
pg_restore: creating ACL "SCHEMA public"

2021-09-26 15:33:42.510 UTC [49] LOG:  received fast shutdown request
waiting for server to shut down....2021-09-26 15:33:42.512 UTC [49] LOG:  aborting any active transactions
2021-09-26 15:33:42.513 UTC [49] LOG:  background worker "logical replication launcher" (PID 56) exited with exit code 1
2021-09-26 15:33:42.514 UTC [51] LOG:  shutting down
2021-09-26 15:33:42.551 UTC [49] LOG:  database system is shut down
server stopped

PostgreSQL init process complete; ready for start up.

2021-09-26 15:33:42.652 UTC [1] LOG:  starting PostgreSQL 13.4 (Debian 13.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-09-26 15:33:42.653 UTC [1] LOG:  listening on IPv4 address "", port 5432
2021-09-26 15:33:42.653 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2021-09-26 15:33:42.658 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-09-26 15:33:42.666 UTC [80] LOG:  database system was shut down at 2021-09-26 15:33:42 UTC
2021-09-26 15:33:42.672 UTC [1] LOG:  database system is ready to accept connections