Python 无法使用 Postgres、Docker Compose 和 Psycopg2 将主机名“db”转换为地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51750715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:55:15  来源:igfitidea点击:

Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2

pythonpostgresqldockerdocker-composepsycopg2

提问by gongarek

In one folder I have 3 files: base.py, Dockerfile and docker-compose.yml.

在一个文件夹中,我有 3 个文件:base.py、Dockerfile 和 docker-compose.yml。

base.py:

基础.py:

import psycopg2

conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'")

Dockerfile:

Dockerfile:

FROM ubuntu:16.04

RUN apt-get update
RUN apt-get -y install python-pip
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install psycopg2-binary

COPY base.py base.py

RUN python base.py

docker-compose.yml:

docker-compose.yml:

version: '3'
services:
  db:
    image: 'postgres:latest'
    expose:
      - "5432"
    environment:
      POSTGRES_PASSWORD: pw1234
      POSTGRES_DB: base123
  aprrka:
    build: .    
    depends_on:
      - db

After I ran docker-compose up, I got the following error:

运行后docker-compose up,出现以下错误:

Traceback (most recent call last):
  File "base.py", line 5, in <module>
conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'")
   File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known

ERROR: Service 'aprrka' failed to build: The command '/bin/sh -c python base.py' returned a non-zero code: 1

I don't know why I have this error. I exposed port 5432. By default Compose sets up a single network for app. Each service joins the default network, I think that my app with postgres should work together. Did I write incorrect docker-compose.yml?

我不知道为什么我有这个错误。我暴露了端口 5432。默认情况下,Compose 为应用程序设置了一个网络。每个服务都加入默认网络,我认为我的应用程序与 postgres 应该一起工作。我写错了 docker-compose.yml 吗?

采纳答案by Hyman Gore

The problem is you should not be running python base.pyas part of the RUNdirective.

问题是你不应该python base.py作为RUN指令的一部分运行。

The RUNdirective is executed only when you are building the image. The postgrescontainer is not running at this point, nor has the network been created. Instead you want to use the CMDdirective.

RUN指令仅在您构建映像时执行。此时postgres容器没有运行,也没有创建网络。相反,您想使用该CMD指令。

Change the Dockerfileto this:

改成Dockerfile这样:

FROM ubuntu:16.04

RUN apt-get update
RUN apt-get -y install python-pip
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install psycopg2-binary

COPY base.py base.py

CMD ["python", "base.py"]

The above should result in the hostname dbto be resolved. However if your python code doesn't have any reconnection logic for connecting to the database the container will likely still error out. This because the postgrescontainer will be running but the database won't be ready to accept connections.

以上应该导致主机名db被解析。但是,如果您的 Python 代码没有用于连接到数据库的任何重新连接逻辑,则容器可能仍会出错。这是因为postgres容器将运行,但数据库尚未准备好接受连接。

This can be temporarily fixed by adding restart: alwaysto your docker-compose.yml.

这可以通过添加restart: always到您的docker-compose.yml.

version: '3'
services:
  db:
    image: 'postgres:latest'
    expose:
      - "5432"
    environment:
      POSTGRES_PASSWORD: pw1234
      POSTGRES_DB: base123
  aprrka:
    restart: always
    build: .    
    depends_on:
      - db

Hopefully this will get you up and running.

希望这能让你启动并运行。

回答by helpfulT

if you add this to your db container in your docker-compose.yml it should resolve the issue

如果您将此添加到您的 docker-compose.yml 中的 db 容器中,它应该可以解决问题

environment:
  - "POSTGRES_HOST_AUTH_METHOD=trust"

回答by Nivratti Boyane

Add network, link and depends_onconfiguration in docker compose file.

dockercompose 文件中添加网络、链接和depends_on配置。

something like this:

像这样:

  services:
      db:
          build: .
          container_name: db
          networks:
              - djangonetwork
      web:
          build: .
          depends_on:
             - db
          links:
             - db:db
          networks:
             - djangonetwork

  networks:
      djangonetwork:
          driver: bridge

the above configuration helped me to resolve the host name to connect to the db.

上述配置帮助我解析主机名以连接到数据库。

回答by X Sham

Another possible scenario,

另一种可能的情况,

Check if ports have been used or not by other docker container. Use command:

检查端口是否已被其他 docker 容器使用。使用命令:

$ docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" -a

Then change your ports/expose in docker-compose file

然后在 docker-compose 文件中更改您的端口/暴露

回答by YasirAzgar

Changing the host from "db" to "postgres" worked for me.

将主机从“db”更改为“postgres”对我有用。

Also use postgres defaults for username and password which is

还对用户名和密码使用 postgres 默认值

username: postgres
password: