psql:无法翻译主机名“ somePostgres”,地址:未知的名称或服务 [英] psql: could not translate host name "somePostgres" to address: Name or service not known

查看:680
本文介绍了psql:无法翻译主机名“ somePostgres”,地址:未知的名称或服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在docker中构建java spring mvc应用程序,而ockefile构建涉及与postgres容器进行交互。每当我运行 docker-compose up 时,dockerfile中与海报交互的步骤有时会因异常而失败

I am building a java spring mvc application in docker and dockefile build involves interacting with postgres container. Whenever i run docker-compose up the step in dockerfile which interacts with the postrges sometimes fails with an exception


psql:无法将主机名 somePostgres转换为地址:名称或服务未知
失败

psql: could not translate host name "somePostgres" to address: Name or service not known FAILED

失败:生成失败并显示

DockerCompose文件:

DockerCompose file:

abcdweb:
  links:
  - abcdpostgres
  build: .
  ports:
  - "8080:8080"
  volumes:
  - .:/abcd-myproj
  container_name: someWeb
abcdpostgres:
  image: postgres
  environment:
  - POSTGRES_PASSWORD=postgres
  - POSTGRES_USER=postgres
  container_name: somePostgres

somePostgres似乎很快启动,并且没有延迟加载postgres容器问题。目前,我正在由docker-machine创建的虚拟框中运行它。

The somePostgres seems to start very quickly and There is no late loading of postgres container problem. Currently i am running this in virtual box created by docker-machine. Unable to get error as it's not persistent.

PS:添加了Dockerfile

PS: Added Dockerfile

FROM java:7
RUN apt-get update && apt-get install -y postgresql-client-9.4
ADD . ./abcd-myproj
WORKDIR /abcd-myproj
RUN ./gradlew build -x test 
RUN sh db/importdata.sh
CMD ./gradlew jettyRun


推荐答案

此错误的基本含义是psql无法解析主机名,请改用ip地址。

Basically what this error means is that psql was unable to resolve the host name, try using the ip address instead.

https://github.com/postgres/postgres/blob/313f56ce2d1b9dfd3483e4f39611baa27852835a/src/interfaces/libpq/fe-connect.c#L2275-L2285

case CHT_HOST_NAME:
    ret = pg_getaddrinfo_all(ch->host, portstr, &hint,
                             &conn->addrlist);
    if (ret || !conn->addrlist)
    {
        appendPQExpBuffer(&conn->errorMessage,
                          libpq_gettext("could not translate host name \"%s\" to address: %s\n"),
                          ch->host, gai_strerror(ret));
        goto keep_going;
    }
break;

https://github.com/postgres/postgres/blob/8255c7a5eeba8f1a38b7a431c04909bde4f5e67d/src/common/ip.c#L57-L75

int
pg_getaddrinfo_all(const char *hostname, const char *servname,
                   const struct addrinfo *hintp, struct addrinfo **result)
{
    int         rc;

    /* not all versions of getaddrinfo() zero *result on failure */
    *result = NULL;

#ifdef HAVE_UNIX_SOCKETS
    if (hintp->ai_family == AF_UNIX)
        return getaddrinfo_unix(servname, hintp, result);
#endif

    /* NULL has special meaning to getaddrinfo(). */
    rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
                     servname, hintp, result);

    return rc;
}

这篇关于psql:无法翻译主机名“ somePostgres”,地址:未知的名称或服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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