Search code examples
djangopostgresqldocker

DJango and Docker: db shutting down and web service not accepting connections


I have a problem with Docker. I am trying to build a docker postgres DB and web service for a DJango app. Here is my config: Dockerfile:

# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

docker-compose.yml:

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=whatsarounddb
      - POSTGRES_USER=dbadmin
      - POSTGRES_PASSWORD=dbadmin123
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=whatsarounddb
      - POSTGRES_USER=dbadmin
      - POSTGRES_PASSWORD=dbadmin123
    depends_on:
      - db

settings.py from Django:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

**code**

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'whatsarounddb',
        'USER': 'dbadmin',
        'PASSWORD': 'dbadmin321',
        'HOST': 'localhost',
        'PORT': 5432,
    }
}

When I use docker-compose up --build I get the following output:

[+] Running 14/1
 ✔ db 13 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                                                                                                            9.6s 
[+] Building 7.0s (14/14) FINISHED                                                                                                                                                                                      docker:default
 => [web internal] load .dockerignore                                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                                                   0.0s 
 => [web internal] load build definition from Dockerfile                                                                                                                                                                          0.0s 
 => => transferring dockerfile: 234B                                                                                                                                                                                              0.0s 
 => [web] resolve image config for docker.io/docker/dockerfile:1                                                                                                                                                                  1.5s 
 => [web auth] docker/dockerfile:pull token for registry-1.docker.io                                                                                                                                                              0.0s
 => CACHED [web] docker-image://docker.io/docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31be0021                                                                                             0.0s
 => [web internal] load metadata for docker.io/library/python:3                                                                                                                                                                   1.1s
 => [web auth] library/python:pull token for registry-1.docker.io                                                                                                                                                                 0.0s
 => [web 1/5] FROM docker.io/library/python:3@sha256:89f4c413ac0f36072211bced42ff7e8870cf5347c3cde4b84a67b5f87911b9a3                                                                                                             0.0s
 => [web internal] load build context                                                                                                                                                                                             0.6s 
 => => transferring context: 20.18MB                                                                                                                                                                                              0.5s
 => CACHED [web 2/5] WORKDIR /code                                                                                                                                                                                                0.0s 
 => CACHED [web 3/5] COPY requirements.txt /code/                                                                                                                                                                                 0.0s 
 => CACHED [web 4/5] RUN pip install -r requirements.txt                                                                                                                                                                          0.0s 
 => [web 5/5] COPY . /code/                                                                                                                                                                                                       0.7s 
 => [web] exporting to image                                                                                                                                                                                                      0.3s 
 => => exporting layers                                                                                                                                                                                                           0.3s 
 => => writing image sha256:84508890bedfbd07d8a82287364751d6f6c003878668251fa07a87e946e2161d                                                                                                                                      0.0s 
 => => naming to docker.io/library/whats_around-web                                                                                                                                                                               0.0s 
[+] Running 3/3
 ✔ Network whats_around_default  Created                                                                                                                                                                                          0.0s 
 ✔ Container whats_around-db-1   Created                                                                                                                                                                                          0.0s 
 ✔ Container whats_around-web-1  Created                                                                                                                                                                                          0.0s 
Attaching to whats_around-db-1, whats_around-web-1
whats_around-db-1   | 
whats_around-db-1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
whats_around-db-1   | 
whats_around-db-1   | 2023-11-20 09:59:34.361 UTC [1] LOG:  starting PostgreSQL 16.1 (Debian 16.1-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
whats_around-db-1   | 2023-11-20 09:59:34.361 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
whats_around-db-1   | 2023-11-20 09:59:34.361 UTC [1] LOG:  listening on IPv6 address "::", port 5432
whats_around-db-1   | 2023-11-20 09:59:34.393 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
whats_around-db-1   | 2023-11-20 09:59:34.481 UTC [29] LOG:  database system was shut down at 2023-11-20 02:19:40 UTC
whats_around-db-1   | 2023-11-20 09:59:34.559 UTC [1] LOG:  database system is ready to accept connections
whats_around-web-1  | Watching for file changes with StatReloader
whats_around-web-1  | Performing system checks...
whats_around-web-1  | 
whats_around-web-1  | System check identified no issues (0 silenced).
whats_around-web-1  | Exception in thread django-main-thread:
whats_around-web-1  | Traceback (most recent call last):
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
whats_around-web-1  |     self.connect()
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
whats_around-web-1  |     return func(*args, **kwargs)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 270, in connect
whats_around-web-1  |     self.connection = self.get_new_connection(conn_params)
whats_around-web-1  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
whats_around-web-1  |     return func(*args, **kwargs)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
whats_around-web-1  |     connection = self.Database.connect(**conn_params)
whats_around-web-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect
whats_around-web-1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  | psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
whats_around-web-1  |   Is the server running on that host and accepting TCP/IP connections?
whats_around-web-1  | connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
whats_around-web-1  |   Is the server running on that host and accepting TCP/IP connections?
whats_around-web-1  | 
whats_around-web-1  | 
whats_around-web-1  | The above exception was the direct cause of the following exception:
whats_around-web-1  | 
whats_around-web-1  | Traceback (most recent call last):
whats_around-web-1  |   File "/usr/local/lib/python3.12/threading.py", line 1052, in _bootstrap_inner
whats_around-web-1  |     self.run()
whats_around-web-1  |   File "/usr/local/lib/python3.12/threading.py", line 989, in run
whats_around-web-1  |     self._target(*self._args, **self._kwargs)
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
whats_around-web-1  |     fn(*args, **kwargs)
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 136, in inner_run
whats_around-web-1  |     self.check_migrations()
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 574, in check_migrations
whats_around-web-1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
whats_around-web-1  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/migrations/executor.py", line 18, in __init__
whats_around-web-1  |     self.loader = MigrationLoader(self.connection)
whats_around-web-1  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/migrations/loader.py", line 58, in __init__
whats_around-web-1  |     self.build_graph()
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/migrations/loader.py", line 235, in build_graph
whats_around-web-1  |     self.applied_migrations = recorder.applied_migrations()
whats_around-web-1  |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
whats_around-web-1  |     if self.has_table():
whats_around-web-1  |        ^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/migrations/recorder.py", line 57, in has_table
whats_around-web-1  |     with self.connection.cursor() as cursor:
whats_around-web-1  |          ^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
whats_around-web-1  |     return func(*args, **kwargs)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 330, in cursor
whats_around-web-1  |     return self._cursor()
whats_around-web-1  |            ^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 306, in _cursor
whats_around-web-1  |     self.ensure_connection()
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
whats_around-web-1  |     return func(*args, **kwargs)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
whats_around-web-1  |     with self.wrap_database_errors:
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
whats_around-web-1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
whats_around-web-1  |     self.connect()
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
whats_around-web-1  |     return func(*args, **kwargs)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 270, in connect
whats_around-web-1  |     self.connection = self.get_new_connection(conn_params)
whats_around-web-1  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
whats_around-web-1  |     return func(*args, **kwargs)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
whats_around-web-1  |     connection = self.Database.connect(**conn_params)
whats_around-web-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  |   File "/usr/local/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect
whats_around-web-1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
whats_around-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
whats_around-web-1  | django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
whats_around-web-1  |   Is the server running on that host and accepting TCP/IP connections?
whats_around-web-1  | connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
whats_around-web-1  |   Is the server running on that host and accepting TCP/IP connections?
whats_around-web-1  | 

I noticed that the db is shutting down all of a sudden:

whats_around-db-1   | 2023-11-20 09:59:34.481 UTC [29] LOG:  database system was shut down at 2023-11-20 02:19:40 UTC
whats_around-db-1   | 2023-11-20 09:59:34.559 UTC [1] LOG:  database system is ready to accept connections

Then the web service fails asking if I can receive connections.

I really don't know how to solve this problem. I'm totally lost at the moment. Any help will be a blessing.

Thank you for your time and patience! Happy coding!


Solution

  • You have error in DATABASES HOST setting, it should be

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'whatsarounddb',
            'USER': 'dbadmin',
            'PASSWORD': 'dbadmin321',
            'HOST': 'db',
            'PORT': 5432,
        }
    }
    

    because localhost refers to a local network of a specific container and postgres is not running there, so when it tries to connect, it throws an error. Postgres is running in a separate container, and it is available through docker network under the name 'db'