Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 379
Varis Darasirikul Avatar asked May 07 '20 17:05

Varis Darasirikul


2 Answers

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

like image 56
Trolie Avatar answered Sep 20 '22 15:09

Trolie


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.

like image 25
Viviane Costa Avatar answered Sep 19 '22 15:09

Viviane Costa