【问题标题】:Docker container connecting to Postgres database连接到 Postgres 数据库的 Docker 容器
【发布时间】:2023-04-02 14:32:01
【问题描述】:

将我的 Django docker 容器连接到我的 Postgres 数据库时,我收到以下错误。

    File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 
    130, in connect conn = _connect(dsn, 
    connection_factory=connection_factory, **kwasync)
    django.db.utils.OperationalError: could not connect to server: 
    Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
    could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

下面是我运行容器的 Dockerfile

   FROM python:3.6

    MAINTAINER c15523957

    RUN apt-get -y update
    RUN apt-get -y upgrade

    RUN apt-get -y install libgdal-dev

    RUN mkdir -p /usr/src/app

    COPY requirements.txt /usr/src/app/
    COPY . /usr/src/app

    WORKDIR /usr/src/app

    RUN pip install --upgrade pip
    RUN pip install --no-cache-dir -r requirements.txt

   EXPOSE 8000
   CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

这是我的 Django 代码的 settings.py 文件中的代码

    DATABASES = {
        'default': {
           'ENGINE': 'django.contrib.gis.db.backends.postgis',
           'NAME': 'place_loc_db',
           'USER': 'place_loc_user',
          'PASSWORD': 'abc123',
          'HOST': 'localhost',
          'PORT': 5432,
  }

}

注意:我没有将 postgres 数据库作为容器运行,而是在我的本地计算机上运行

注意:以下详细信息包含在我的 pg_hba.conf 和 postgresql.conf 文件中

postgresql.conf->listen_addresses = '*'

pg_hba.conf -> host all all 0.0.0.0/0 md5

我已阅读上述打开数据库连接的以下详细信息。

【问题讨论】:

标签: postgresql docker dockerfile


【解决方案1】:

您似乎正在尝试连接到localhost。您的 Postgres 数据库没有在与您的 django 应用程序相同的容器中运行,因此您将无法在 localhost 找到它。您需要将您的应用指向 Postgres 容器的地址。

如果您在用户定义的网络中运行容器(使用 docker-compose 会自动为您执行此操作),那么您可以使用容器名称作为主机名。

container networking 上的文档是一个很好的起点。

更新

您在主机上运行 Postgres 这一事实并没有显着改变答案:您仍然需要将您的 web 应用程序指向 Postgres 服务器的地址,而不是 localhost

最简单的方法取决于您是在 Linux 上本地运行 Docker,还是运行 Docker-For-X,其中 X 是 MacOS 或 Windows。

在 Linux 上,只需将您的 webapp 指向 docker0 接口的 IP 地址。这是您主机的一个地址,并且由于您已将 Postgres 配置为侦听所有接口,因此应该可以正常工作。

如果您使用的是 Mac 或 Windows,则有一个特殊的“魔术主机名”指代您主机上的服务。例如,阅读this 了解 MacOS 下的详细信息。在这两种情况下,您都可以将您的 web 应用程序指向 host.docker.internal

【讨论】:

  • 感谢您的回复。我实际上没有 postgres 容器,很抱歉没有在问题中指定这一点。这个 postgres 数据库在我的本地计算机上。有什么方法可以连接到我的本地 Postgres 数据库,或者我必须为它制作一个容器。谢谢
  • 正如@larsks 建议的那样,查看 docker 网络文档以了解容器网络的工作原理。您不需要制作 postgresql 容器,但 django 容器中的 'localhost' 就是容器本身。尝试连接到您电脑的真实 IP。
  • 在我的例子中,api 在 docker 容器中运行,而 postgres 在 localhost 上运行。错误是“无法分配请求的地址 5432”。问题是连接字符串默认指向本地主机。我已将其更改为“host.docker.internal”-“IdentityServerDb”:“Server=host.docker.internal;Port=5432;Database=postgres;UserId=postgres;Password=xx;”它奏效了。
【解决方案2】:
  1. 确保已完成

    postgresql.conf->listen_addresses = '*'

    pg_hba.conf -> host all all 0.0.0.0/0 md5

  2. brew services 启动 postgresql(在 MAC 中,重新加载 postgres 服务)。

  3. 获取您的系统IP。
  4. 在您的数据库连接中使用您的系统 IP 地址而不是 localhost。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2021-09-11
    • 2015-09-23
    • 2016-07-20
    • 2022-11-20
    相关资源
    最近更新 更多