Docker containers can not connect to each other

I have a django-rest-framwork app which uses PosgreSql as db. So I am using docker containers for them one image for django-rest-framwork and one for PosgreSql, and then docker compose file to handle them.

  • db service refrese to PostgreSQL
  • backend refers to Django Rest app

I have a docker-compose file

version: '3.9'
services:
  
  db:
    image: postgres:latest
    restart: always
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=85842475DB
      - POSTGRES_DB=sports_center_db
      - POSTGRES_PORT=5432
  
  backend:
    build: ./api
    depends_on:
      - db
    ports:
      - "8000:8000"
    environment:
      - DB_NAME=sports_center_db
      - DB_USER_NAME=postgres
      - DB_PASSWORD=85842475DB
      - DB_HOST=db
      - DB_PORT=5432

It builds correctly but when I ran docker compose up I got following logs

[+] Running 2/2
 ⠿ Container sports_center-db-1       Created                                                                                                   0.1s
 ⠿ Container sports_center-backend-1  Created                                                                                                   0.1s
Attaching to sports_center-backend-1, sports_center-db-1
sports_center-db-1       | The files belonging to this database system will be owned by user "postgres".
sports_center-db-1       | This user must also own the server process.
sports_center-db-1       | 
sports_center-db-1       | The database cluster will be initialized with locale "en_US.utf8".
sports_center-db-1       | The default database encoding has accordingly been set to "UTF8".
sports_center-db-1       | The default text search configuration will be set to "english".
sports_center-db-1       | 
sports_center-db-1       | Data page checksums are disabled.
sports_center-db-1       | 
sports_center-db-1       | fixing permissions on existing directory /var/lib/postgresql/data ... ok
sports_center-db-1       | creating subdirectories ... ok
sports_center-db-1       | selecting dynamic shared memory implementation ... posix
sports_center-db-1       | selecting default max_connections ... 100
sports_center-db-1       | selecting default shared_buffers ... 128MB
sports_center-db-1       | selecting default time zone ... Etc/UTC
sports_center-db-1       | creating configuration files ... ok
sports_center-db-1       | running bootstrap script ... ok
sports_center-db-1       | performing post-bootstrap initialization ... ok
sports_center-backend-1  | Exception in thread django-main-thread:
sports_center-backend-1  | Traceback (most recent call last):
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 282, in ensure_connection
sports_center-backend-1  |     self.connect()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
sports_center-backend-1  |     return func(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 263, in connect
sports_center-backend-1  |     self.connection = self.get_new_connection(conn_params)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
sports_center-backend-1  |     return func(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
sports_center-backend-1  |     connection = Database.connect(**conn_params)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/psycopg2/__init__.py", line 122, in connect
sports_center-backend-1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sports_center-backend-1  | psycopg2.OperationalError: connection to server at "db" (172.18.0.2), port 5432 failed: Connection refused
sports_center-backend-1  |  Is the server running on that host and accepting TCP/IP connections?
sports_center-backend-1  | 
sports_center-backend-1  | 
sports_center-backend-1  | The above exception was the direct cause of the following exception:
sports_center-backend-1  | 
sports_center-backend-1  | Traceback (most recent call last):
sports_center-backend-1  |   File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
sports_center-backend-1  |     self.run()
sports_center-backend-1  |   File "/usr/lib/python3.10/threading.py", line 953, in run
sports_center-backend-1  |     self._target(*self._args, **self._kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/autoreload.py", line 64, in wrapper
sports_center-backend-1  |     fn(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/core/management/commands/runserver.py", line 137, in inner_run
sports_center-backend-1  |     self.check_migrations()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/core/management/base.py", line 564, in check_migrations
sports_center-backend-1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/executor.py", line 18, in __init__
sports_center-backend-1  |     self.loader = MigrationLoader(self.connection)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/loader.py", line 58, in __init__
sports_center-backend-1  |     self.build_graph()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/loader.py", line 235, in build_graph
sports_center-backend-1  |     self.applied_migrations = recorder.applied_migrations()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
sports_center-backend-1  |     if self.has_table():
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/recorder.py", line 57, in has_table
sports_center-backend-1  |     with self.connection.cursor() as cursor:
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
sports_center-backend-1  |     return func(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 323, in cursor
sports_center-backend-1  |     return self._cursor()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 299, in _cursor
sports_center-backend-1  |     self.ensure_connection()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
sports_center-backend-1  |     return func(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 281, in ensure_connection
sports_center-backend-1  |     with self.wrap_database_errors:
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/utils.py", line 91, in __exit__
sports_center-backend-1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 282, in ensure_connection
sports_center-backend-1  |     self.connect()
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
sports_center-backend-1  |     return func(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 263, in connect
sports_center-backend-1  |     self.connection = self.get_new_connection(conn_params)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
sports_center-backend-1  |     return func(*args, **kwargs)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
sports_center-backend-1  |     connection = Database.connect(**conn_params)
sports_center-backend-1  |   File "/usr/local/lib/python3.10/dist-packages/psycopg2/__init__.py", line 122, in connect
sports_center-backend-1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sports_center-backend-1  | django.db.utils.OperationalError: connection to server at "db" (172.18.0.2), port 5432 failed: Connection refused
sports_center-backend-1  |  Is the server running on that host and accepting TCP/IP connections?
sports_center-backend-1  | 
sports_center-db-1       | syncing data to disk ... ok
sports_center-db-1       | 
sports_center-db-1       | 
sports_center-db-1       | Success. You can now start the database server using:
sports_center-db-1       | 
sports_center-db-1       |     pg_ctl -D /var/lib/postgresql/data -l logfile start
sports_center-db-1       | 
sports_center-db-1       | initdb: warning: enabling "trust" authentication for local connections
sports_center-db-1       | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
sports_center-db-1       | waiting for server to start....2022-10-16 19:57:48.569 UTC [48] LOG:  starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
sports_center-db-1       | 2022-10-16 19:57:48.575 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
sports_center-db-1       | 2022-10-16 19:57:48.596 UTC [51] LOG:  database system was shut down at 2022-10-16 19:57:41 UTC
sports_center-db-1       | 2022-10-16 19:57:48.607 UTC [48] LOG:  database system is ready to accept connections
sports_center-db-1       |  done
sports_center-db-1       | server started
sports_center-db-1       | CREATE DATABASE
sports_center-db-1       | 
sports_center-db-1       | 
sports_center-db-1       | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
sports_center-db-1       | 
sports_center-db-1       | waiting for server to shut down...2022-10-16 19:57:48.814 UTC [48] LOG:  received fast shutdown request
sports_center-db-1       | .2022-10-16 19:57:48.820 UTC [48] LOG:  aborting any active transactions
sports_center-db-1       | 2022-10-16 19:57:48.823 UTC [48] LOG:  background worker "logical replication launcher" (PID 54) exited with exit code 1
sports_center-db-1       | 2022-10-16 19:57:48.824 UTC [49] LOG:  shutting down
sports_center-db-1       | 2022-10-16 19:57:48.830 UTC [49] LOG:  checkpoint starting: shutdown immediate
sports_center-db-1       | 2022-10-16 19:57:49.416 UTC [49] LOG:  checkpoint complete: wrote 918 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.047 s, sync=0.516 s, total=0.592 s; sync files=250, longest=0.009 s, average=0.003 s; distance=4217 kB, estimate=4217 kB
sports_center-db-1       | 2022-10-16 19:57:49.442 UTC [48] LOG:  database system is shut down
sports_center-db-1       |  done
sports_center-db-1       | server stopped
sports_center-db-1       | 
sports_center-db-1       | PostgreSQL init process complete; ready for start up.
sports_center-db-1       | 
sports_center-db-1       | 2022-10-16 19:57:49.580 UTC [1] LOG:  starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
sports_center-db-1       | 2022-10-16 19:57:49.581 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
sports_center-db-1       | 2022-10-16 19:57:49.581 UTC [1] LOG:  listening on IPv6 address "::", port 5432
sports_center-db-1       | 2022-10-16 19:57:49.593 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
sports_center-db-1       | 2022-10-16 19:57:49.609 UTC [63] LOG:  database system was shut down at 2022-10-16 19:57:49 UTC
sports_center-db-1       | 2022-10-16 19:57:49.621 UTC [1] LOG:  database system is ready to accept connections
sports_center-db-1       | 2022-10-16 20:02:49.708 UTC [61] LOG:  checkpoint starting: time
sports_center-db-1       | 2022-10-16 20:02:53.897 UTC [61] LOG:  checkpoint complete: wrote 44 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=4.131 s, sync=0.029 s, total=4.190 s; sync files=12, longest=0.006 s, average=0.003 s; distance=252 kB, estimate=252 kB

As I understand containers can not connect to each other. Any error or if I missing something here in the compose file, please tell me why this is happening and how to solve it.

EDIT: as suggested by BMitch, maybe you need to create a network.

Initial answer:

Maybe you need to link them ?

Try adding the following to the backend:

    links:
      - db

Your application started before the database finished initializing. Depends_on waits for the other container to start, but there's still a delay before it's available. There are several possible solutions:

  1. Configure the app to retry connecting to the database.

  2. Configure the app to wait for the database using a command like wait-for-it.

  3. Adjust the depends_on condition to also wait on the healthcheck:

version: "2.4"
services:
  web:
    build: .
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
  redis:
    image: redis
  db:
    image: postgres
    healthcheck:
      test: "exit 0"
Back to Top