0

first of all i apologize for my bad English :)

I've tied to set up an environment with docker-compose containing a loopback API, a PostgreSQL database and several other services.

Here you can see the docker-compose.yml

version: "2.1"
services:
  postgresql:
    image: postgres:9.6
    restart: always
    environment:
      - "COMPOSE_HTTP_TIMEOUT=200000"
      - "POSTGRES_USER=root"
      - "POSTGRES_PASSWORD=root"
      - "POSTGRES_DB=hh"
  backend:
    image: tyrex/backend
    restart: always
    stdin_open: true
    tty: true
    environment:
      - "API_BASE_PATH=/api"
      - "HOST_NAME=haaye-henricus.de"
      - "PORT=3000"
      - "DATABASE_HOST=postgresql"
      - "DATABASE_PORT=5432"
      - "DATABASE_NAME=hh"
      - "DATABASE_USER=root"
      - "DATABASE_PASSWORD=root"
      - "DEBUG_DATABASE_CONNECTOR=true"
    ports:
      - "3000:3000"
    command: ["./wait-for-it.sh", "postgresql:5432", "--", "node", "."]

Here you can see the datasources.local.js

  // Copyright IBM Corp. 2014,2015. All Rights Reserved.
  // Node module: loopback-example-offline-sync
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT

  'use strict';

  var DATABASE_HOST = process.env.DATABASE_HOST || 'localhost';
  var DATABASE_USER = process.env.DATABASE_USER || 'root';
  var DATABASE_PASSWORD = process.env.DATABASE_PASSWORD || 'root';
  var DATABASE_PORT = process.env.DATABASE_PORT || '5432';
  var DATABASE_NAME = process.env.DATABASE_NAME || 'hh';
  var DEBUG_DATABASE_CONNECTOR = process.env.DEBUG_DATABASE_CONNECTOR || false;
  var FILES_DIRECTORY = process.env.FILES_DIRECTORY || './storage';

  console.log('Database Url', 'postgresql://' + DATABASE_USER + ':' + DATABASE_PASSWORD + '@' + DATABASE_HOST +  ':' + DATABASE_PORT  + '/' + DATABASE_NAME);

  console.log('New Build');

  module.exports = {
    'db': {
      'name': 'db',
      'connector': 'memory',
    },
    'postgresql': {
      'url': 'postgresql://' + DATABASE_USER + ':' + DATABASE_PASSWORD + '@' + DATABASE_HOST +  ':' + DATABASE_PORT  + '/' + DATABASE_NAME,
      'debug': DEBUG_DATABASE_CONNECTOR,
      'name': 'postgresql',
      'connector': 'postgresql',
    },
    'files_datasource': {
      'name': 'files_datasource',
      'connector': 'loopback-component-storage',
      'provider': 'filesystem',
      'root': FILES_DIRECTORY,
      'nameConflict': 'makeUnique',
    },
  };

When i try to run docker-compose up i'll get the following output

events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRNOTAVAIL 54.201.47.166:5432
    at Object.exports._errnoException (util.js:1050:11)
    at exports._exceptionWithHostPort (util.js:1073:20)
    at Server.setupListenHandle [as _listen2] (net.js:1243:19)
    at listenInCluster (net.js:1307:12)
    at doListen (net.js:1432:7)
    at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:10)

I've stepped into the container and sent a ping to "postgresql" it results into 0% packet lost but pinging 54.201.47.166 gets 100% packet lost

Searching the whole www results in nothing :(

Please can somebody help me

  • "postgresql" points to which ip? do nslookup u will get ip and change process.env.DATABASE_HOST=postgresql in js file – sanath meti Nov 24 '17 at 13:04
  • Thank you. If done that. Now i get `Error: connect ECONNREFUSED 172.19.0.4:5432` – Tjerk Dames Nov 24 '17 at 14:07
  • ok means its connecting to postgres docker, make changes to pg_hba.conf and postgresql.conf and also provide port for postgres in yml. follow this to connect postgres from host &container https://stackoverflow.com/questions/47423173/postgresql-in-docker-pg-hba-conf-to-allow-access-from-host-to-container/47431144?noredirect=1#comment81825870_47431144 – sanath meti Nov 24 '17 at 14:46

1 Answers1

0

Try to give the postgres service a container name with container_name: postgresql & then ping the postgresql service instead of the container's IP in the application container. Make sure that the DATABASE_HOST matches with the container_name. Sample Example MEAN application docker-compose file here.

Janshair Khan
  • 2,577
  • 4
  • 20
  • 44
  • Thank you for answering :) I executed a bash on the BackendContainer in there i pinged postgres and got 0% packet lost. But Loopback is not using the right ip.. Loopback is querying 54.201.47.166. If i execute `nslookup postgress` i got 172.19.0.4. So in conclusion the postgresscontainer is reachable from the BackendContainer, but the backendcontainer is using the wrong ip. – Tjerk Dames Nov 24 '17 at 20:12
  • Connecting containers through IP is a bit tedious approach, use embedded DNS instead. – Janshair Khan Nov 25 '17 at 11:51