Docker 图片在 "Got an error reading communication packets" 后消失

Docker image dies after "Got an error reading communication packets"

我目前面临着一种我无法理解的非常奇怪的行为。我的设置是:一个微服务,使用节点 10.18.1 和 knex js v0.20.10,用于访问 mysql 5.7 db

的数据库

错误 对于本地测试,我有一个 docker-compose 文件,用于创建包括我的项目在内的所有图像。我遇到的问题是,每次我尝试从数据库中获取信息时,我的项目图像都会被杀死。查看日志后,我看到来自 mysql 的以下消息:

mysql    | 2020-03-12T00:40:18.457996Z 2 [Note] Aborted connection 2 to db: <DB_NAME> user: 'root' host: '172.18.0.3' (Got an error reading communication packets)

我能够将问题缩小到以下代码部分:

async function getUser (condition) {
    console.log('>> here', condition)

    const users = await knexClient(TABLE_NAME).where(condition) //<-- all good here

    const user = users[0]  //<-- still nothing wrong
    console.log('>', user) // <-- i can see the user from the DB
    await sleep(10000)  // <-- It can even wait for 10 s
    return users.pop()  // <-- BOOM! 
    // return null // <-- If i return null, there is no error!?!?!? wtf?
}
```

函数调用

async function getByEmail (email) {
      **const user = await getUser({ email })**

      if (user == null) {
        throw applicationErrors.notFoundError('User not found or is not activated')
      }

      return user
  }

基本上我面临的问题是,每次我尝试 return 在数据库中找到的用户时,我都会收到 mysql 错误(读取通信数据包时出错)。发生此错误后,我的 docker 图像停止,因为它正在退出,状态代码为 0。

有趣的事实

当我运行同样的源代码,同样的Knex配置,指向本地mysqldocker图片时,不会出现这个错误!

我的 docker 文件如下:

FROM node:10-alpine
ADD . /app
WORKDIR /app
RUN npm i --only=prod

RUN rm -rf ./config/local.json
RUN rm -rf ./docker-compose.override.yml

CMD ["npm", "start"]

我的 docker-compose 看起来像这样:

version: "3"

services:
  app:
    build: .
    container_name: <SERVICE_NAME>
    ports:
      - "9000:9000"
    # expose:
    #   - 9000
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7.13
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: unless-stopped
    volumes:
      - mysql:/var/lib/mysql
      - "./infrastructure/docker/initdb.sql:/docker-entrypoint-initdb.d/init.sql"
    environment:
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - "3306:3306"
    # expose:
    #   - 3306

volumes:
  mysql:

任何帮助或建议,我们将不胜感激!

只是一个大胆的猜测,但有时本地依赖项会影响行为。按照建议尝试忽略 node_modules 文件夹 here