1

I am currently having some problems with docker and connecting a node project with Postgres when running inside it.

I have already asked a related question see link, however, I can’t wrap my head around connection issue between nodeJs and postgres.

My docker-compose file looks like:

# docker-compose.yml
version: "2.1"

services:
  app:
    build: .
    ports:
      - "49160:8080"
    networks:
      - webnet
    depends_on:
      db:
        condition: service_healthy
    environment:
      DATABASE_URL: postgres://postgres:taskin@db:5432/mydb


  db:
    image: kartoza/postgis:9.6-2.4
    networks: 
      - webnet
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=mypassword
      - POSTGRES_MULTIPLE_EXTENSIONS=postgis,pgrouting
      - ALLOW_IP_RANGE=0.0.0.0/0
    volumes:
      - pgdata:/var/lib/postgresql/data

    restart: on-failure
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

networks:
  webnet:

volumes:
  pgdata: {}

my Dockerfile has the following input:

#node 8
FROM node:8

#Create app directory
WORKDIR /usr/src/app

#Install app dependencies
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . . 

EXPOSE 8080

CMD ["npm", "start"]

my nodeJS-express server has following settings:

var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://postgres:mypassword@db:5432/mydb";

var client = new pg.Client(conString);

client.connect()

When I run docker-compose up

  1. I first get a password authentification error: I could fix this with editing the pg_hba.conf and postgresql.conf as described in this link link

  2. After restarting PostgreSQL and changing the password for postgres user I restored my SQL backup file into mydb within the container

  3. then I stopped previous docker-compose command and executed sudo docker-compose up again. Get following output

db_1_56896493481e | 
db_1_56896493481e | postgres conf already configured
db_1_56896493481e | ssl already configured
db_1_56896493481e | pg_hba  already configured
db_1_56896493481e | Setup master database
db_1_56896493481e | 2019-07-04 18:21:23.452 UTC [22] LOG:  database system was interrupted; last known up at 2019-07-04 18:14:49 UTC
db_1_56896493481e | 2019-07-04 18:21:23.470 UTC [30] postgres@postgres FATAL:  the database system is starting up
db_1_56896493481e | psql: FATAL:  the database system is starting up
db_1_56896493481e | 2019-07-04 18:21:23.508 UTC [22] LOG:  database system was not properly shut down; automatic recovery in progress
db_1_56896493481e | 2019-07-04 18:21:23.511 UTC [22] LOG:  redo starts at 0/28ECC738
db_1_56896493481e | 2019-07-04 18:21:23.511 UTC [22] LOG:  invalid record length at 0/28ECC770: wanted 24, got 0
db_1_56896493481e | 2019-07-04 18:21:23.511 UTC [22] LOG:  redo done at 0/28ECC738
db_1_56896493481e | 2019-07-04 18:21:23.520 UTC [22] LOG:  MultiXact member wraparound protections are now enabled
db_1_56896493481e | 2019-07-04 18:21:23.522 UTC [35] LOG:  autovacuum launcher started
db_1_56896493481e | 2019-07-04 18:21:23.522 UTC [17] LOG:  database system is ready to accept connections
db_1_56896493481e |                                  List of databases
db_1_56896493481e |        Name       |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
db_1_56896493481e | ------------------+----------+----------+---------+---------+-----------------------
db_1_56896493481e |  postgres         | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
db_1_56896493481e |  mydb  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
db_1_56896493481e |  template0        | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
db_1_56896493481e |                   |          |          |         |         | postgres=CTc/postgres
db_1_56896493481e |  template1        | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
db_1_56896493481e |                   |          |          |         |         | postgres=CTc/postgres
db_1_56896493481e |  template_postgis | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
db_1_56896493481e | (5 rows)
db_1_56896493481e | 
db_1_56896493481e | postgres ready
db_1_56896493481e | Postgis Already There
db_1_56896493481e | HSTORE is only useful when you create the postgis database.
db_1_56896493481e | TOPOLOGY is only useful when you create the postgis database.
db_1_56896493481e | Setup postgres User:Password
db_1_56896493481e | ALTER ROLE
db_1_56896493481e | Check default db exists
db_1_56896493481e | mydb db already exists
db_1_56896493481e |                                  List of databases
db_1_56896493481e |        Name       |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
db_1_56896493481e | ------------------+----------+----------+---------+---------+-----------------------
db_1_56896493481e |  postgres         | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
db_1_56896493481e |  mydb  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
db_1_56896493481e |  template0        | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
db_1_56896493481e |                   |          |          |         |         | postgres=CTc/postgres
db_1_56896493481e |  template1        | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
db_1_56896493481e |                   |          |          |         |         | postgres=CTc/postgres
db_1_56896493481e |  template_postgis | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
db_1_56896493481e | (5 rows)
db_1_56896493481e | 
db_1_56896493481e | 2019-07-04 18:21:24.742 UTC [17] LOG:  received smart shutdown request
db_1_56896493481e | 2019-07-04 18:21:24.742 UTC [35] LOG:  autovacuum launcher shutting down
db_1_56896493481e | 2019-07-04 18:21:24.743 UTC [32] LOG:  shutting down
db_1_56896493481e | 2019-07-04 18:21:24.847 UTC [17] LOG:  database system is shut down
db_1_56896493481e | 
db_1_56896493481e | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1_56896493481e | 
db_1_56896493481e | Postgres initialisation process completed .... restarting in foreground
db_1_56896493481e | 2019-07-04 18:21:25.886 UTC [129] LOG:  database system was shut down at 2019-07-04 18:21:24 UTC
db_1_56896493481e | 2019-07-04 18:21:25.888 UTC [129] LOG:  MultiXact member wraparound protections are now enabled
db_1_56896493481e | 2019-07-04 18:21:25.891 UTC [133] LOG:  autovacuum launcher started
db_1_56896493481e | 2019-07-04 18:21:25.891 UTC [126] LOG:  database system is ready to accept connections
app_1_a393ffc30f68 | 
app_1_a393ffc30f68 | > node-api-postgres@1.0.0 start /usr/src/app
app_1_a393ffc30f68 | > node index.js
app_1_a393ffc30f68 | 
app_1_a393ffc30f68 | Running on http://0.0.0.0:8080




Candy
  • 95
  • 1
  • 10
  • `app_1_ca885d4626de | wait-for-it.sh: waiting 15 seconds for db:5432`: maybe wait a bit more, since the first error happened about 1 second before `db_1_ceb54072343c | 2019-07-04 12:23:14.921 UTC [18] LOG: database system is ready to accept connections`? – Stock Overflaw Jul 04 '19 at 14:34
  • Now I am getting another Error meassage: `Error: connect ECONNREFUSED 172.20.0.2:5432 ` – Candy Jul 04 '19 at 14:38
  • Also, according to [this](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) you can use `healthcheck` (never used it) and mainly `depends_on`. When you got it running, it'd be a good idea to move this `wait-for-it.sh` logic to the composer. – Stock Overflaw Jul 04 '19 at 14:39
  • How long did you wait for it? I'd say, to validate the error really comes from the DB not being ready, you should wait like 120 seconds (your log shouldn't show any more `db`-related stuff when Node boots up, that way you're sure whether the problem comes from that lack of synchronization between the 2 containers). – Stock Overflaw Jul 04 '19 at 14:41
  • I removed tha wait-for-it.sh command, and replaced it with depends_on: db in my docker compose file, but still getting `Error: getaddrinfo ENOTFOUND db db:5432 ` – Candy Jul 04 '19 at 15:54
  • Check the SO link I gave above: `depends_on` only checks if the container is alive (i.e. _not_ checking whether what;s running in it is alive). In this post, `depends_on` takes a `condition: service_healthy`, and the targeted container (`db` in your case) is checked upon with a `healthcheck` section that instructs the composer how to assert the _contained_ program is healthy (i.e. properly running). – Stock Overflaw Jul 04 '19 at 16:04
  • Other than that, your `networks` seems OK, so `db` should be properly converted to target the `db` container, and the image you use already exposes port 5432. – Stock Overflaw Jul 04 '19 at 16:06
  • I followed the link (see edits above in question), but still when I run localhost:49160/ and fire my get request, I doesn't get an output... – Candy Jul 04 '19 at 18:33
  • I even changed the `time` to `40s` and the `interval` to 40s , but no success.... – Candy Jul 04 '19 at 18:49

1 Answers1

0

After trying a lot, I could fix it. My docker-compose file was correct, my ajax request was not properly defined. After changing

 $.ajax({
    url: 'http://db:8080/show',
    type: "POST",
    data: CoordjsonObject,
    dataType: "json",
    success: function(data) {
      addStreetsLayer(data);
      console.log("worked!")
      },   
    error: function(xhr) {
      console.log(xhr)
      }
  });

to

 $.ajax({
    url: 'http://IP-ADRESS-OF-NODE-CONTAINER:8080/show',
    type: "POST",
    data: CoordjsonObject,
    dataType: "json",
    success: function(data) {
      addStreetsLayer(data);
      console.log("worked!")
      },   
    error: function(xhr) {
      console.log(xhr)
      }
  });
Candy
  • 95
  • 1
  • 10