无法翻译主机名“db"解决使用 Postgres、Docker Compose 和 Psycopg2 [英] Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2

查看:22
本文介绍了无法翻译主机名“db"解决使用 Postgres、Docker Compose 和 Psycopg2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

base.py:

import psycopg2

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

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:

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

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

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

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

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

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?

推荐答案

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

The problem is you should not be running python base.py as part of the RUN directive.

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

The RUN directive is executed only when you are building the image. The postgres container is not running at this point, nor has the network been created. Instead you want to use the CMD directive.

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"]

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

The above should result in the hostname db to 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 postgres container will be running but the database won't be ready to accept connections.

这可以通过将 restart: always 添加到您的 docker-compose.yml 来临时修复.

This can be temporarily fixed by adding restart: always to your 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.

这篇关于无法翻译主机名“db"解决使用 Postgres、Docker Compose 和 Psycopg2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆