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

Var*_*kul 14 node.js sequelize.js google-cloud-sql google-cloud-platform docker-compose

我正在使用 typescript、express 和Sequelize.

这是我的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;
  }
}
Run Code Online (Sandbox Code Playgroud)

所以在我的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);
});
Run Code Online (Sandbox Code Playgroud)

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

但是当我尝试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
Run Code Online (Sandbox Code Playgroud)

这是我的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"]
Run Code Online (Sandbox Code Playgroud)

但问题是当我跑步时 docker-compose up --build

我只看到这些日志

api_1  | Init db
api_1  | start connect
api_1  | Server running in development mode on port 5000
Run Code Online (Sandbox Code Playgroud)

所以基本上没有sequelize.authenticate()成功或失败的响应,只需Server running in development mode on port 5000注销即可。

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

谢谢

小智 17

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

  • 哦天哪,谢谢你。我也遇到了同样的问题,我想我快疯了。知道了罪魁祸首,我发现了一个 [sequelize 的 GitHub 上的问题。](https://github.com/sequelize/sequelize/issues/12158) 该冲突是由于过时的 pg 依赖项导致的,该依赖项由于节点中未记录的更改而中断 - [更多解释和参考在这里](https://github.com/codimd/server/commit/d6ce60c86e714b9835822e8539470b21b8fb823c) - 如果你将 pg 升级到 8.0.3 你不需要降级节点 (6认同)
  • 我使用的是 Node 版本 14,在 Mac 上也遇到了完全相同的问题。降级到 12.6.0 有效 (2认同)