简体   繁体   English

psycopg2.OperationalError:无法使用 psycopg2 和 docker 将主机名转换为地址

[英]psycopg2.OperationalError: could not translate host name to address using psycopg2 and docker

I've been trying for a few hours now but no solution from similar asked questions seem to work for me... I am using docker-compose to setup a postgresql database and run a python webserver from where I want to connect to my postgressql database (so it's running inside the container)我已经尝试了几个小时,但类似问题的解决方案似乎对我没有用......我正在使用 docker-compose 设置 postgresql 数据库并运行 python 网络服务器,从 Igre 连接到 mysql 数据库所以它在容器内运行)

version: '3.8'

services:
  database:
    container_name: database
    hostname: database
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    volumes:
      - postgres:/pgdata
      - ./application/ressources/fixtures.sql:/docker-entrypoint-initdb.d/fixtures.sql 
    ports:
      - "5432:5432"

  application:
    container_name: application
    build: .
    ports:
      - "5001:5001"
    volumes: 
      - ./application:/application
    restart: always
    depends_on:
      - database

volumes:
    postgres:

I trying to connect as follows ( I have read that despite the depends on in my dockerfile the database needs some more time until it can accept connections so i added a retry logic):我尝试按如下方式连接(我已经读过尽管在我的 dockerfile 中依赖于数据库需要更多时间才能接受连接,所以我添加了重试逻辑):


        retries = 0
        while retries < 5:
            retries = retries + 1
            self.conn = psycopg2.connect(user='postgres', password='password',
                                         host='database', port="5432", database='mydatabase')
            if not self.conn:
                logging.info("retry to connect")
                sleep(5)

The weird thing is that when running it with docker-compose -f docker-compose.yml up everything works fine.奇怪的是,当使用docker-compose -f docker-compose.yml up运行它时,一切正常。 But when I built the image ( docker build -t myapp:0.1 ) and run it ( docker run myapp:0.1 ) it gives me the following error:但是当我构建图像( docker build -t myapp:0.1 )并运行它( docker run myapp:0.1 )它给了我以下错误:

File "/application/libraries/database.py", line 18, in establishConnection
    self.conn = psycopg2.connect(user=CONFIG.DATABASE_USER, password=CONFIG.DATABASE_PASSWORD,
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "database" to address: Name or service not known

I've read that when using docker-compose a single network is created, so this can't be the error here i guess Docker Compose doku我读过使用 docker-compose 时会创建一个网络,所以这不可能是这里的错误我猜Docker Compose doku

Thanks in advance, Jacky在此先感谢,杰基

If you run docker run on an image, it does only what's on that command line and no more.如果您在映像上运行docker run ,它只会执行该命令行上的内容,而不会执行更多操作。 In particular, the plain docker commands don't know about docker-compose.yml and any of the settings you might specify there.特别是,普通的docker命令不知道docker-compose.yml以及您可能在那里指定的任何设置。

The short answer is to always use docker-compose up to launch the containers.简短的回答是始终使用docker-compose up来启动容器。

In principle you could translate the docker-compose.yml file into explicit docker commands.原则上,您可以将docker-compose.yml文件转换为显式docker命令。 At a minimum you'd need to manually create a Docker network and specify the container names:您至少需要手动创建一个 Docker 网络并指定容器名称:

docker network create app-net
docker run -d --net app-net --name database -p 5432:5432 postgres
docker run -d --net app-net -e PGHOST=database -p 5001:5001 myapp:0.1

This hasn't included the other options in the Compose setup, though, notably database persistence.不过,这并未包括 Compose 设置中的其他选项,尤其是数据库持久性。 There are enough settings that you'd want to write them down in a file, and at that point you've basically reconstructed the Compose environment.有足够多的设置需要您将它们写在一个文件中,此时您基本上已经重建了 Compose 环境。

暂无
暂无

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

相关问题 Heroku + Postgres:psycopg2.OperationalError:无法翻译主机名“无” - Heroku + Postgres: psycopg2.OperationalError: could not translate host name “None” 无法使用 Postgres、Docker Compose 和 Psycopg2 将主机名“db”转换为地址 - Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2 psycopg2.OperationalError:无法将主机名“xxxxxx.us-east-1.rds.amazonaws.com”转换为地址:未知主机 - psycopg2.OperationalError: could not translate host name "xxxxxx.us-east-1.rds.amazonaws.com" to address: Unknown host psycopg2.OperationalError 错误 - psycopg2.OperationalError errors Docker- django 在连接到 postgres 时抛出错误:psycopg2.OperationalError:无法连接到服务器:连接被拒绝 - Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused (psycopg2.OperationalError) 无效 - 操作码 - (psycopg2.OperationalError) Invalid - opcode 数据库不存在(psycopg2.OperationalError) - DATABASE NOT EXIST (psycopg2.OperationalError) (psycopg2.OperationalError)无法连接到服务器:连接被拒绝是服务器 - (psycopg2.OperationalError) could not connect to server: Connection refused Is the server Sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) - Sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) psycopg2.OperationalError:严重:数据库不存在 - psycopg2.OperationalError: FATAL: database does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM