简体   繁体   English

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

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

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.我暴露了端口 5432。默认情况下,Compose 为应用程序设置了一个 single.network。 Each service joins the default.network, I think that my app with postgres should work together.每个服务都加入 default.network,我认为我的应用程序与 postgres 应该一起工作。 Did I write incorrect docker-compose.yml?我写错了 docker-compose.yml 吗?

The problem is you should not be running python base.py as part of the RUN directive.问题是您不应该将python base.py作为RUN指令的一部分RUN

The RUN directive is executed only when you are building the image. RUN指令仅在您构建映像时执行。 The postgres container is not running at this point, nor has the network been created. postgres容器此时没有运行,也没有创建网络。 Instead you want to use the CMD directive.相反,您想使用CMD指令。

Change the Dockerfile to 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 db to be resolved.以上应该导致主机名db被解析。 However if your python code doesn't have any reconnection logic for connecting to the database the container will likely still error out.但是,如果您的 Python 代码没有用于连接到数据库的任何重新连接逻辑,则容器可能仍会出错。 This because the postgres container will be running but the database won't be ready to accept connections.这是因为postgres容器将运行,但数据库不会准备好接受连接。

This can be temporarily fixed by adding restart: always to 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.希望这能让你启动并运行。

Add database and web service on same network in docker compose file.在 docker compose 文件中在同一网络上添加数据库和 Web 服务。 Also link db service and start web service after starting db service.启动db服务后还要链接db服务并启动web服务。

After properly adding network, link and depends_on configuration in docker compose file issue will be fixed.正确添加网络后,docker compose 文件中的link 和depends_on配置问题将得到修复。

Configuration example:配置示例:

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

  networks:
      djangonetwork:
          driver: bridge

In my docker compose file, I have used network name as 'djangonetwork', you can use any other name.在我的 docker compose 文件中,我使用了网络名称作为“djangonetwork”,您可以使用任何其他名称。

the above configuration helped me to resolve the issue 'Could not translate host name db'.上述配置帮助我解决了“无法翻译主机名 db”的问题。

If you're using docker-compose first add this line to your .yml:如果您使用 docker-compose 首先将此行添加到您的 .yml:

environment:
  - "POSTGRES_HOST_AUTH_METHOD=trust"

After that you start only you db docker:之后,您只启动您的 db docker:

docker-compose up db

It should start normally with a message like:它应该以如下消息正常启动:

Recreating db... done
Attaching to db
db_1           | ********************************************************************************
db_1           | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow
db_1           |          anyone with access to the Postgres port to access your database without
db_1           |          a password, even if POSTGRES_PASSWORD is set. See PostgreSQL
db_1           |          documentation about "trust":
db_1           |          https://www.postgresql.org/docs/current/auth-trust.html
db_1           |          In Docker's default configuration, this is effectively any other
db_1           |          container on the same system.
db_1           | 
db_1           |          It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace
db_1           |          it with "-e POSTGRES_PASSWORD=password" instead to set a password in
db_1           |          "docker run".
db_1           | ********************************************************************************

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"

In one folder I have 3 files: base.py, Dockerfile and docker-compose.yml.在一个文件夹中,我有3个文件:base.py,Dockerfile和docker-compose.yml。

base.py: base.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.我暴露了端口5432。默认情况下,Compose为应用设置单个网络。 Each service joins the default network, I think that my app with postgres should work together.每个服务都加入了默认网络,我认为我的应用程序与postgres应该一起工作。 Did I write incorrect docker-compose.yml?我写了不正确的docker-compose.yml吗?

Another possible scenario,另一种可能的情况,

Check if ports have been used or not by other docker container.检查端口是否已被其他 docker 容器使用。 Use command:使用命令:

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

Then change your ports/expose in docker-compose file然后在 docker-compose 文件中更改您的端口/暴露

I thought I'd give an updated answer as I recently found a similar issue.我想我会给出一个更新的答案,因为我最近发现了一个类似的问题。

I had a similar setup where the app would connect correctly from docker running locally, but would fail when running in Jenkins.我有一个类似的设置,其中应用程序可以从本地运行的 docker 正确连接,但在 Jenkins 中运行时会失败。

Changing image: postgres:latest to image: postgres:13.4-alpine solved the issue.image: postgres:latest更改为image: postgres:13.4-alpine解决了这个问题。

Not sure if you are happy using IPs, but it seems like a good solution to me.不确定您是否喜欢使用 IP,但这对我来说似乎是一个很好的解决方案。 Changing all hostnames to IPs made it work and seems secure enough将所有主机名更改为 IP 使其工作并且看起来足够安全

https://localcoder.org/psql-could-not-translate-host-name-somepostgres-to-address-name-or-service-n https://localcoder.org/psql-could-not-translate-host-name-somepostgres-to-address-name-or-service-n

"Solution 1: “解决方案 1:
Basically what this error means is that psql was unable to resolve the host name, try using the ip address instead."基本上这个错误意味着 psql 无法解析主机名,请尝试使用 ip 地址代替。”

暂无
暂无

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

相关问题 psycopg2.OperationalError:无法使用 psycopg2 和 docker 将主机名转换为地址 - psycopg2.OperationalError: could not translate host name to address using psycopg2 and docker Docker 运行,在 docker-compose 构建后无法将主机名“db”转换为地址:名称或服务未知 - Docker run, after docker-compose build could not translate host name “db” to address: Name or service not known Heroku + Postgres:psycopg2.OperationalError:无法翻译主机名“无” - Heroku + Postgres: psycopg2.OperationalError: could not translate host name “None” Docker:当使用容器名称从一个容器连接到另一个容器时,如何修复“无法将主机名”postgres“转换为地址”? - Docker: How to fix “could not translate host name ”postgres“ to address” when connecting from one container to another using container name? Django Docker:django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称或服务未知 - Django Docker: django.db.utils.OperationalError: could not translate host name “db” to address: Name or service not known Docker django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称解析暂时失败 - Docker django.db.utils.OperationalError: could not translate host name “db” to address: Temporary failure in name resolution 无法将主机名“postgres”转换为地址:未知主机 - could not translate host name “postgres” to address: Unknown host 通过 PyCharm 在 Docker 中运行 Django 测试时,无法将主机名“db”转换为地址 - could not translate host name "db" to address when running Django test in Docker through PyCharm 无法将主机名“db”转换为地址:名称解析暂时失败 - could not translate host name "db" to address: Temporary failure in name resolution Docker:运行容器时“无法将主机名“plagdb”转换为地址” - Docker: "could not translate host name "plagdb" to address" when running a container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM