postgresql Django连接到Docker中的Postgres失败

c86crjj0  于 5个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(82)

我正在使用macOS,用Postgres构建一个Django应用程序,我想用Docker容器化它。
我有以下文件:
Dockerfile:

FROM python:3.11
WORKDIR /medicin_api
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python3", "./manage.py", "runserver", "8001"]

字符串
docker-compose.yaml:

version: latest
services:
  db_api:
    image: postgres:16
    restart: always
    container_name: db_api
    ports:
      - "5433:5433"
    environment:
      - POSTGRES_USER=api_user
      - POSTGRES_DB=medicin_patients
      - POSTGRES_PASSWORD=Baz#Dan&Api!
    volumes:
      - postgres:/data/postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U api_user -d medicin_patients"]
      interval: 5s
      timeout: 2s
      retries: 10

  api:
    container_name: api
    build: .
    command: python manage.py runserver 0.0.0.0:8001
    volumes:
      - .:/api_volume
    ports:
      - "8001:8001"
    depends_on:
      db_api:
        condition: service_healthy

volumes:
  postgres:


在django settings.py中设置PostgreSQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'medicin_patients',
        'USER': 'api_user',
        'PASSWORD': 'Baz#Dan&Api!',
        'HOST': 'db_api',
        'PORT': '5433'
    }
}


当我运行命令:docker-compose up -d --build时,我得到了两个图像和容器,其中:

  • db_api容器与PostgreSQL数据库一起正常运行,我可以通过CLI访问它
  • api容器正在运行,但它抛出了下面的错误,我无法解决:
2023-12-10 12:32:25 Watching for file changes with StatReloader
2023-12-10 12:32:25 Exception in thread django-main-thread:
2023-12-10 12:32:25 Traceback (most recent call last):
2023-12-10 12:32:25   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 275, in ensure_connection
2023-12-10 12:32:25     self.connect()
2023-12-10 12:32:25   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
2023-12-10 12:32:25     return func(*args, **kwargs)
2023-12-10 12:32:25            ^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
2023-12-10 12:32:25     self.connection = self.get_new_connection(conn_params)
2023-12-10 12:32:25                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
2023-12-10 12:32:25     return func(*args, **kwargs)
2023-12-10 12:32:25            ^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25   File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
2023-12-10 12:32:25     connection = self.Database.connect(**conn_params)
2023-12-10 12:32:25                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25   File "/usr/local/lib/python3.11/site-packages/psycopg/connection.py", line 745, in connect
2023-12-10 12:32:25     raise last_ex.with_traceback(None)
2023-12-10 12:32:25 psycopg.OperationalError: connection failed: Connection refused
2023-12-10 12:32:25     Is the server running on that host and accepting TCP/IP connections?


我在这里错过了什么,或者错误地设置了我的Django应用程序没有连接到PostgreSQL?

5ssjco0h

5ssjco0h1#

看起来你描述的问题是由你从PostgreSQL容器暴露的错误端口引起的。如上所述,PostgreSQL默认在端口5432上运行。所以,有两个选项:
1.通过向db_api容器添加新的环境变量PGPORT: 5433来更改默认端口。
1.由于您的主机上的端口5432已被占用,因此您可以通过更改端口Map来更改Map,而无需更改Posgresql示例的配置:- "5433:5432"

相关问题