Django, postgress docker-compose psycopg2.OperationalError
Установка: сервисы postgres, django и redis настроены через docker-compose.
Я прочитал похожие вопросы на SO, но не могу понять, почему моя конфигурация compose приводит к тому, что django не может увидеть службу db. Служба db внутренне настроена на порт 5432 в сети compose, но отображена на 5433 на хост-машине (5432 на хосте используется другим docker db контейнером). Я не уверен, что я здесь упускаю, надеюсь, что кто-то может увидеть что-то в конфигурации или журналах, чего я не вижу...
журналы ошибок от веб-сервиса:
stage_web_0.10 | django.db.utils.OperationalError: could not connect to server: Connection refused
stage_web_0.10 | Is the server running on host "db" (172.25.0.2) and accepting
stage_web_0.10 | TCP/IP connections on port 5432?
Настройки Django изнутри контейнера (правильно ищет db по адресу 5432):
settings.DATABASES
{'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'db',
'PORT': '5432',
'NAME': 'stage',
'USER': 'admin',
'TEST': {'NAME': 'test_stage'}}}
docker-compose services
db:
container_name: stage_${DJANGO_ENV}_db_${TAG}
env_file:
- .env
build:
context:
./db/
args: # Make these available at build time to create the new db.
- POSTGRES_HOST
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_PORT
volumes:
- /data/postgresql/stage_${DJANGO_ENV}_${TAG}/:/var/lib/postgresql/data/
environment:
- TAG
- DJANGO_ENV
- POSTGRES_HOST
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_PORT
ports:
- "5433:5432"
expose:
- "5432" # Publishes 5432 to other containers but NOT to host machine
web:
container_name: stage_${DJANGO_ENV}_web_${TAG}
build:
context:
./web
dockerfile: Dockerfile.stage
args:
- PIP_INDEX_URL
- PIP_TRUSTED_HOST
env_file:
- .env
# Command gets appended to the entrypoint in the dockerfile and will be run
# when all containers are up (need postgres container started for migrations).
command: /code/start-django.sh
volumes:
- ./web/web_application/:/code/web
- ${STORAGE_PATH_HOST}:${STORAGE_PATH_CONTAINER}
# certificates
- /etc/apache2/ssl:/etc/apache2/ssl
ports:
- 8080:8000
depends_on:
- db
- redis
Записи для контейнера db:
Attaching to stage_db_0.10
stage_db_0.10 | 2022-04-25 16:44:16.734 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
stage_db_0.10 | 2022-04-25 16:44:16.734 UTC [1] LOG: listening on IPv6 address "::", port 5432
stage_db_0.10 | 2022-04-25 16:44:16.782 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
stage_db_0.10 | 2022-04-25 16:44:16.867 UTC [19] LOG: database system was shut down at 2022-04-25 16:42:54 UTC
stage_db_0.10 | 2022-04-25 16:44:16.941 UTC [1] LOG: database system is ready to accept connections
docker ps показывает правильное отображение для контейнера db
0.0.0.0:5433->5432/tcp, :::5433->5432/tcp stage_db_0.10
Любая помощь очень ценится!