Docker Compose + Postgres + Nodejs + knex API Connect in Bank

Asked

Viewed 655 times

-1

Hello

I am studying Docker Compose, I made a simple API to make user CRUD, but I want to climb a container to run the Node and one with postgres and they communicating I did the following 'Docker Compose.yml' :

version: "3.3"
services: 
  app:
    build: .
    depends_on: 
      - db
    command: yarn dev
    ports: 
      - "3333:3333"
    volumes: 
      - .:/user/app
    links: 
      - db

  db:
    image: postgres
    restart: always
    ports: 
      - "5433:5432"
    volumes: 
      - /var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: -senha-
      POSTGRES_USER: -user-
    

I left the outer door as the 5433 because I already have another postgres bench running at door 5432

My 'knexfile.js' file looks like this:

// Update with your config settings.

module.exports =  {

  development: {
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3'
    }
  },

  staging: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user:     'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  },

  production: {
    client: 'postgresql',
    connection: {
      port: 5433,
      database: 'nome banco',
      user:     'user',
      password: 'senha'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  }

};

when I execute the docker-compose up it creates and performs very well if I try to communicate with the API with Insomnia works, if I try to communicate with the container postgres with Dbeaver works the same, but the API does not communicate with the bank, the error:

{
  "errno": -111,
  "code": "ECONNREFUSED",
  "syscall": "connect",
  "address": "127.0.0.1",
  "port": 5433
}


What am I doing wrong so they won’t communicate? I’ve searched a lot about it and found nothing.

2 answers

1

There are some problems with your knexfile.js.

The recommended default lib to use with knex is lib pg

Then you would have to modify in your file the client line to have the value:

client: 'pg',

This lib that is trying to use even exists but I do not recommend, since the last version released is 6 years ago and the lib is in version 0.0.1.

Another problem is that in your file knexfile.js is missing the host line, according to the knex documentation a full connection Adapter for postgres would be:

var knex = require('knex')({
  client: 'pg',
  version: '7.2',
  connection: {
    host : '127.0.0.1', //<--adicionar esta linha
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test',
    port: 5432
  }
}); 

When this line is omitted the knex uses the default value that is 127.0.0.1.

Only that your database is running on your Docker instance, so you should modify the host to:

host : 'db'

Which is the name of your database instance in the file docker-compose.yml.

The final knexfile.js would be:

var knex = require('knex')({
  client: 'pg',
  version: '7.2',
  connection: {
    host : 'db', //<--adicionar esta linha
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test',
    port: 5432
  }
}); 

Check that I have swapped the lib from the database then it is necessary to install the lib pg. Currently this lib is in the version 8.3.0 (08/2020). In the knex documentation he recommends 7.2 (maybe they use this version (7.2) because of compatibility with knex query).

  • danizavtz thank you very much for the answer, that was part of the problem, I had actually already installed the pg but I wasn’t using it in the knex because I thought it was postgresql kkkk

  • and the other part was the host: I had no idea that I had to put the same name generated in the Compose but I noticed something that the tdvias tmb answered below the door inside the container has to be the 5432 because the one that I set 5433 is the port to access from outside the Docker network

1


Since the error shows that you tried using port 5433, I believe you are using the configuration production and trying to "connect" one container directly to the other. In this case, the port that should be used is the internal one (5432) and not the "exposed" one on the host. The same goes for the server, which should be "db" (the name of your container in Docker Compose). Since it is not specified, I assume that the driver assumes the value 127.0.0.1 (according to the error message). You would then need to specify it, otherwise your application will try to connect to a database within the same container.

  • Thanks tdvias, worse than yesterday I had tested put the same ip of the bank container, in theory it was to work but I was still using the door 5433, only after reading your reply that I realized that the door had to be 5432 because the containers were inside the network of the Docker n of my PC itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.