简体   繁体   English

Docker容器连接到Postgres数据库

[英]Docker container connecting to Postgres database

I am receiving the following error when connecting my Django docker container to my Postgres database. 将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?

Below is my Dockerfile which run the container 以下是我的运行容器的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"]

Here is the code in my settings.py file of my Django code 这是我的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,
  }

} }

Note: I do not have the postgres database running as a container but on my local computer 注意:我没有将postgres数据库作为容器运行,而是在本地计算机上运行

Note: The following details are contained in my pg_hba.conf and postgresql.conf files 注意:以下详细信息包含在我的pg_hba.conf和postgresql.conf文件中

postgresql.conf - > listen_addresses = '*' postgresql.conf- > listen_addresses ='*'

pg_hba.conf - > host all all 0.0.0.0/0 md5 pg_hba.conf- >全部托管0.0.0.0/0 md5

I've read that the following details above open connections on the database. 我已经阅读到上面的以下详细信息在数据库上打开了连接。

It looks like you're trying to connect to localhost . 看来您正在尝试连接到localhost Your Postgres database is notrunning inside the same container as your django app, so you're not going to be able to find it at localhost . 您的Postgres数据库未在与django应用程序相同的容器中运行,因此您将无法在localhost上找到它。 You need to point your app at the address of the Postgres container. 您需要将您的应用程序指向Postgres容器的地址。

If you run your containers in a user defined network (using docker-compose will do this for you automatically), then you can use container name as hostnames. 如果您在用户定义的网络中运行容器(使用docker-compose会自动为您执行此操作),则可以将容器名称用作主机名。

The documentation on container networking is a good place to start. 有关容器网络的文档是一个不错的起点。

Update 更新

The fact that you're running Postgres on your host doesn't substantially change the answer: you still need to point your webapp at the address of the Postgres server, rather than localhost . 您在主机上运行Postgres的事实并没有真正改变答案:您仍然需要将webapp指向Postgres服务器的地址,而不是localhost

The easiest way of doing that depends on whether or not you're running Docker natively on Linux, or you're running Docker-For-X, where X is MacOS or Windows. 最简单的方法取决于您是在Linux上本地运行Docker还是运行Docker-For-X,其中X是MacOS或Windows。

On Linux, just point your webapp at the ip address of the docker0 interface. 在Linux上,只需将您的docker0应用程序指向docker0接口的IP地址docker0 This is an address of your host, and since you have Postgres configured to listen on all interfaces, this should work out just fine. 这是您主机地址,并且由于已将Postgres配置为在所有接口上进行侦听,因此应该可以正常工作。

If you're on Mac or Windows, there is a special "magic hostname" that refers to services on your host. 如果您使用的是Mac或Windows,则有一个特殊的“魔术主机名”,它指代主机上的服务。 Eg, read this for details under MacOS. 例如,读为MacOS下细节。 In both cases, you can point your webapp at host.docker.internal . 在这两种情况下,您都可以将webapp指向host.docker.internal

  1. Make sure this is done 确保完成

    postgresql.conf- > listen_addresses = '*' postgresql.conf-> listen_addresses ='*'

    pg_hba.conf - > host all all 0.0.0.0/0 md5 pg_hba.conf->全部托管0.0.0.0/0 md5

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

  3. Get your system Ip. 获取您的系统IP。
  4. Use your system ip address instead of localhost in your DB connection. 在数据库连接中使用系统IP地址而不是localhost。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM