简体   繁体   English

在 docker 中使用 jest 连接 ECONNREFUSED 127.0.0.1:3306

[英]connect ECONNREFUSED 127.0.0.1:3306 with using jest in docker

I'm trying to use jest codes before deploy to dev.我正在尝试在部署到开发人员之前使用笑话代码。 So i made docker-compose.yml and put "npm test ( ENV=test jest --runInBand --forceExit test/**.test.ts -u )" but it has error.所以我制作了 docker-compose.yml 并输入“npm test ( ENV=test jest --runInBand --forceExit test/**.test.ts -u )”但它有错误。

This is my local.yml file (for docker-compose.yml)这是我的 local.yml 文件(对于 docker-compose.yml)

version: "3"

services:
  my-node:
    image: my-api-server:dev
    container_name: my_node
    # sleep 10 sec for db init
    command: bash -c "sleep 10; pwd; cd packages/server; yarn orm schema:sync -f ormconfig.dev.js; yarn db:migrate:run -f ormconfig.dev.js; npm test; cross-env ENV=dev node lib/server.js"
    ports:
      - "8082:8082"
    depends_on:
      - my-mysql
      - my-redis

  my-mysql:
    image: mysql:5.7
    container_name: my_mysql
    command: --character-set-server=utf8mb4 --sql_mode="NO_ENGINE_SUBSTITUTION" 
    ports:
      - "33079:3306"
    volumes:
      - ./init/:/docker-entrypoint-initdb.d/
    environment:
      - MYSQL_ROOT_PASSWORD=test
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
   
  my-redis:
    image: redis:6.2-alpine
    container_name: my_redis
    ports:
      - 6379:6379
    command: redis-server --requirepass test        

networks:
  default:
    external:
      name: my_nginx_default

and it's my ormconfig.dev.js file这是我的 ormconfig.dev.js 文件

module.exports = {
    type: 'mysql',
    host: 'my_mysql',
    port: 3306,
    username: 'test',
    password: 'test',
    database: 'test',
    entities: ['./src/modules/**/entities/*'],
    migrations: ['migration/dev/*.ts'],
    cli: { "migrationsDir": "migration/dev" }
}

But after i use docker-compose -f res/docker/local.yml up , it throws error after whole build and then jests.但是在我使用docker-compose -f res/docker/local.yml up ,它会在整个构建后抛出错误然后开玩笑。 and then it opens server which does not have error.然后它打开没有错误的服务器。

Errors are like these below.错误如下所示。

connect ECONNREFUSED 127.0.0.1:3306

and then接着

my_node     |     TypeError: Cannot read property 'subscriptionsPath' of undefined
my_node     |
my_node     |       116 | ): Promise<TestResponse<T>> => {
my_node     |       117 |   const req = request(server.app)
my_node     |     > 118 |     .post(server.apolloServer!.subscriptionsPath!)
my_node     |           |                                ^
my_node     |       119 |     .set('Accept', 'application/json')
my_node     |       120 |
my_node     |       121 |   if (token) {

I've tried to change entities path.我试图改变实体路径。

  • entities: ['./src/modules/**/entities/*']实体:['./src/modules/**/entities/*']
  • entities: ['src/modules/**/entities/*']实体:['src/modules/**/entities/*']
  • entities: [__dirname + '/src/modules/**/entities/*']实体:[__dirname + '/src/modules/**/entities/*']

My entities are in the right path.我的实体走在正确的道路上。

实体路径

Here is my whole file structure这是我的整个文件结构文件结构

Can anyone help this problem?任何人都可以帮助解决这个问题吗?

in your module.export host:my-mysql在你的 module.export host:my-mysql

Looking at the documentation, it could be an environment variable issue.查看文档,这可能是环境变量问题。 As per the docs, the orm config file used is -根据文档,使用的 orm 配置文件是 -

  1. From the environment variables.从环境变量。 Typeorm will attempt to load the.env file using dotEnv if it exists.如果存在,Typeorm 将尝试使用 dotEnv 加载 .env 文件。 If the environment variables TYPEORM_CONNECTION or TYPEORM_URL are set, Typeorm will use this method.如果设置了环境变量 TYPEORM_CONNECTION 或 TYPEORM_URL,Typeorm 将使用此方法。
  2. From the ormconfig.env.来自 ormconfig.env。
  3. From the other ormconfig.[format] files, in this order: [js, ts, json, yml, yaml, xml].从其他 ormconfig.[format] 文件中,按以下顺序:[js, ts, json, yml, yaml, xml]。

Since you have not defined the first two, it must be defaulting to use the ormconfig.js file.由于您尚未定义前两个,因此必须默认使用 ormconfig.js 文件。 There is no reason it should pick ormconfig.dev.js.它没有理由选择 ormconfig.dev.js。

If you can, change the ormconfig.js file to be this -如果可以,请将 ormconfig.js 文件更改为 -

module.exports = {
    type: 'mysql',
    host: 'my_mysql',
    port: 3306,
    username: 'test',
    password: 'test',
    database: 'test',
    entities: ['./src/modules/**/entities/*'],
    migrations: ['migration/dev/*.ts'],
    cli: { "migrationsDir": "migration/dev" }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [SequelizeConnectionRefusedError]:连接ECONNREFUSED 127.0.0.1:3306 - [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306 SequelizeConnectionRefusedError:连接 ECONNREFUSED 127.0.0.1:3306 - SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 未处理的拒绝 SequelizeConnectionRefusedError:连接 ECONNREFUSED 127.0.0.1:3306 - Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306 - Docker Compose connect ECONNREFUSED 172.18.0.4:3306 Docker Compose 使用 MySQL 和 NodeJS 获取错误 ECONNREFUSED 127.0.0.1:3306 - Docker Compose getting error ECONNREFUSED 127.0.0.1:3306 with MySQL and NodeJS 连接到 mysql 时出现错误“connect ECONNREFUSED 127.0.0.1:3306” - Getting error "connect ECONNREFUSED 127.0.0.1:3306" while connecting to mysql 错误:在 MySQL、Heroku 和 Node.js 上连接 ECONNREFUSED 127.0.0.1:3306 - Error: connect ECONNREFUSED 127.0.0.1:3306 on MySQL, Heroku, and Node.js 节点+ MySQL [ECONNREFUSED 127.0.0.1:3306] - Node + MySQL [ECONNREFUSED 127.0.0.1:3306] Docker NodeJS 与 MySQL 连接 ECONNREFUSED 127.0.0.1:8000 - Docker NodeJS with MySQL connect ECONNREFUSED 127.0.0.1:8000 与节点js的My Sql连接抛出错误“connect ECONNREFUSED 127.0.0.1:3306” - connection to My Sql with node js throws error “connect ECONNREFUSED 127.0.0.1:3306”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM