node.js - Sequelize.authenticate() 对 Docker 内部的 google cloud sql 连接不起作用(没有成功或错误响应)

标签 node.js google-cloud-platform docker-compose sequelize.js google-cloud-sql

我正在使用 typescript、express 和 Sequelize 构建 api 服务器.

这是我的 database连接类。

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;
  }
}

所以在我的 server.ts ,我在叫这个
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);
});

所以在我的本地开发中一切正常

但是当我尝试使用 docker compose 运行它时有了这个配置
// 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

这是我的 Dockerfile.dev文件
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"]

但问题是当我运行 docker-compose up --build
我只看到这些日志
api_1  | Init db
api_1  | start connect
api_1  | Server running in development mode on port 5000

所以基本上没有回应是否sequelize.authenticate()是成功还是失败,
只是Server running in development mode on port 5000注销,毕竟什么都没有。

为什么它不能像在本地那样在 docker 内部工作,我错过了任何配置?

谢谢

最佳答案

你可以试试 node:10 版本
我有同样的问题,但是当将图像更改为 node:10 时,该问题就消失了
希望能帮到你

关于node.js - Sequelize.authenticate() 对 Docker 内部的 google cloud sql 连接不起作用(没有成功或错误响应),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61663848/

相关文章:

node.js - 如何在 Mongoose 中聚合和分组

node.js - 如何让 twitter bootstrap 模式在 node js express js 中工作?

json - 如何阻止 Python 的 Requests 库去除转义字符?

javascript - 解析来自http请求的JSON响应?

python - 如何使用 python 以编程方式获取 GCP 身份 token

docker-compose - Varnish/ docker -组成自定义端口和缓存大小

ubuntu - 将数据集从我的 PC 上传到 Google Cloud Platform 上的虚拟机实例的最佳方式

javascript - Firebase Firestore : How to check if the document exists before writing data?

docker - 无法使用 Nginx 在 Blazor docker compose 配置中转发真实客户端 IP 地址

docker - Nextcloud 与 Docker : Can't create or write into the data directory