14

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: could not translate host name "somePostgres" to address: Name or service not known FAILED

FAILURE: Build failed with an exception.

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

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: 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
PRASANNA SARAF
  • 333
  • 1
  • 4
  • 17
  • So the container name is `somePostgres` and you link to `abcdpostgres`. – BMW Dec 03 '15 at 01:37
  • You cannot link to other running containers in your dockerfile. If you explain what you are trying to do in the Dockerfile I can give you some ideas on how to accomplish what you want. – Paul Becotte Dec 03 '15 at 02:42
  • @BMW yes i want to link to somePostgres – PRASANNA SARAF Dec 03 '15 at 08:59
  • 1
    @PaulBecotte As a matter the dockerfile builds and works fine sometime though this behaviour is not persistent. I Have added dockerfile and you can see ./gradlew build -x test requires container to be up and running as build task involves creating and initializing database.I know that there will various approach to solve this problem but i want to know reason behind this inconsistent behaviour. – PRASANNA SARAF Dec 03 '15 at 09:04
  • I came across this thread looking to fix this error, though my setup is not the same. I am using the AWS RDS, which requires that you use the "endpoint" as the host to connect to the database. Come to find out, after checking the endpoint in the console, it had changed... – Shmack Jan 27 '22 at 18:52
  • If you're connecting to a remote database, This error could also occur when you have a bad internet connection. – kennyozordi Jun 17 '22 at 12:08

3 Answers3

6

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;
}
jmunsch
  • 22,771
  • 11
  • 93
  • 114
1

I think links are not encouraged lately.

But, if you want to have services to communicate over network and explicitly here is the config: You need to configure network an both services to attach to that network. It is something like:

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

In this way the service will communicate via the network with service names as adress.

zhrist
  • 1,169
  • 11
  • 28
  • it worked for me, but I prefer to use networks:app:driver:bridge rather than network:external:true, as explained here in https://stackoverflow.com/questions/60132697/could-not-translate-host-name-postgres-to-address-name-or-service-not-known. – MaxiReglisse Jan 05 '22 at 10:19
0

I had to set my secret_key_base in secrets.yml.

With the incorrect key, my app did not have permission to resolve the database domain.

I'm running a rails app in docker that makes use of secret_key_base. The problem is that I was running the app on the production database using the development environment. The development environment entailed the development secrete_key_base. Once I began using the correct key, I could connect to the database.

The error showed up in my rails container logs as Raven 2.13.0 configured not to capture errors: No host specified, no public_key specified, no project_id specified

See this question for how to set the secret_key_base in secrets.yml

Joshua Swain
  • 571
  • 2
  • 4
  • 22