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

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

我正在 docker 中构建 java spring mvc 应用程序,并且 dokefile 构建涉及与 postgres 容器的交互。每当我 运行 docker-compose up docker 文件中与 postrges 交互的步骤有时会失败并出现异常

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

FAILURE: Build failed with an exception.

DockerCompose 文件:

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 创建的虚拟框中使用它。无法得到错误,因为它不是持久的。

PS:添加了 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

我认为最近不鼓励链接。

但是,如果您想让服务通过网络进行通信,并且这里是明确的配置: 您需要配置网络和两种服务以连接到该网络。它是这样的:

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

这样服务将通过网络以服务名称作为地址进行通信。

基本上这个错误意味着 psql 无法解析主机名,请尝试使用 ip 地址。

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] == '[=11=]') ? NULL : hostname,
                     servname, hintp, result);

    return rc;
}

我必须在 secrets.yml 中设置我的 secret_key_base

使用不正确的密钥,我的应用没有解析数据库域的权限。

我是 运行 docker 中的一个 rails 应用程序,它使用了 secret_key_base。问题是我是 运行 使用开发环境的生产数据库上的应用程序。开发环境需要开发secrete_key_base。一旦我开始使用正确的密钥,我就可以连接到数据库。

错误在我的 rails 容器日志中显示为 Raven 2.13.0 configured not to capture errors: No host specified, no public_key specified, no project_id specified

请参阅 了解如何在 secrets.yml

中设置 secret_key_base