Sequelize.authenticate() do not working (no success or error response) for google cloud sql connection inside Docker
Asked Answered
M

2

17

I am building api server with typescript ,express and Sequelize.

This is my database connection class.

export class Database {
  private _sequelize: Sequelize;
  private config: DBConfigGroup = dbConfig;
  private env = process.env.NODE_ENV as string;

  constructor() {
    this._sequelize = new Sequelize({
      dialect: 'postgres',
      database: this.config[this.env].database,
      username: this.config[this.env].username,
      password: this.config[this.env].password,
      host: this.config[this.env].host,
    });
  }

  async connect(): Promise<void> {
    try {
      console.log('start connect');
      await this._sequelize.authenticate();
      console.log('Connection has been established successfully.'.green.bold);
    } catch (error) {
      console.error('Unable to connect to the database:'.red.bold, error);
    }
  }

  async close(): Promise<void> {
    try {
      await this._sequelize.close();
      console.log('Connection has been close successfully.'.red.bold);
    } catch (error) {
      console.error('Unable to close to the database:'.yellow.bold, error);
    }
  }

  get sequelize(): Sequelize {
    return this._sequelize;
  }
}

So in my server.ts, i was calling this

dotenv.config({
  path: './config/environments/config.env',
});

const database = new Database();
console.log('Init db');
database
  .connect()
  .then(() => {
    console.log('success db ininit');
  })
  .catch((err) => {
    console.log('fail db init: ', err);
  }); 

/**
 * Server Activation
 */
const server = app.listen(PORT, () => {
  console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold);
});

So everythings works fine in my local development

But when i try to run it with docker compose with this config

// docker-compose.yml
version: '3.4'

services:
  api:
    stdin_open: true
    tty: true
    build:
      context: ./api
      dockerfile: Dockerfile.dev
    ports:
      - "5000:5000"
    volumes:
      - /app/node_modules
      - ./api:/app

This is my Dockerfile.dev file

FROM node:alpine

ENV NODE_ENV=development

WORKDIR /app

EXPOSE 5000

COPY package.json package-lock.json* ./ 

RUN npm install && npm cache clean --force

COPY . .

CMD ["npm", "run", "dev:docker"]

But the problem is when i run docker-compose up --build

I only see these logs

api_1  | Init db
api_1  | start connect
api_1  | Server running in development mode on port 5000

So basiccally there is no response whether sequelize.authenticate() is success or fail, just Server running in development mode on port 5000 log out and nothing after all.

Why don't it works inside docker like in the local, any config that i missed?

Thanks

Munos answered 7/5, 2020 at 17:19 Comment(11)
Probably you can't reach the database from there (different network, wrong IP/DNS name, DB not listenening on that interface, firewall rule, whatever), and if you'd wait for a minute or two, you'd get a failure with a timeout error. You didn't show the dbConfig - what is the host/port?Claycomb
@Claycomb host is the ip of google sql and port should not be matter isn't it? Also i can connect successfully with normal localhost connection, think i should missed some config for my docker.Munos
What is in Dockerfile.dev?Pyrosis
@TarunLalwani already in the question.Munos
@VarisDarasirikul, sorry I meant to ask for the environment variables and not the DockerfilePyrosis
Ah sorry I missed that it was about Google Cloud SQL, I thought maybe the SQL server was localhost.Claycomb
@TarunLalwani config is just a simple js object, even i try with const object, still not working anyway.Munos
> host is the ip of google sql Can you share what this IP is? I'm guessing you are not running the cloud_sql_proxy, are you?Polky
@Polky this is the public ip 34.69.33.26 , i doubt that the issue is about sequalize itself, since if i change to mySql (on different instance) with same config, it works fine.Munos
Maybe this helps you then: #38650700Polky
@VarisDarasirikul Recently there was a similar issue reported for Node 14 github.com/sequelize/sequelize/issues/12158, check it. Try updating your pg module versionOleaginous
S
20

You can try node:10 version I have same issue but when change image to node:10, that issue is gone Hope to help you

Simonize answered 14/5, 2020 at 7:49 Comment(7)
It's weird but I did same and it's started working. Do you know the reason behind this?Ephod
@Ephod I think conflict Sequelize and Node versionSimonize
Had the same problem and indeed specifying another version of node was the solution. Mine was node:alpine changed to node:13.12.0-alpine3.10Chenault
oh gosh, thank you. I had the same problem and thought I was going crazy. Knowing the culprit, I found an issue on sequelize's GitHub. The conflict is due to an outdated pg dependency that breaks due to an undocumented change in node - more explanation and references here - if you upgrade pg to 8.0.3 you don't need to downgrade nodeGherardo
I was on node version 14 and had the exact same issue on mac. Downgraded to 12.6.0 it workedLisk
I was going crazy and just saw this. My problem is fixed when I downgraded from node 14 to 12.12.Douzepers
I struggled a lot (7-8 days), but the solution is just changed node version to node:12-12-alpine and pg to 8.0.3. Solution is change versions for node or pg will resolve the issue.Flatwise
M
3

Latest node version v14.15.5 is now throwing an error for this problem. It is instructing to install pg locally

/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:81
    throw new Error(`Please install ${moduleName} package manually`);
          ^

Error: Please install pg package manually

I did that and fixed the problem on my machine.

Montes answered 20/2, 2021 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.