Search code examples
javascriptdockermariadbnestjstypeorm

Nestjs/TypeORM Cannot connect to Docker MariaDB


Whenever I run my docker image containing nodejs 11.10 & mariadb, I get the following error:

[Nest] 16 - 3/12/2019, 11:24:02 AM [TypeOrmModule] Unable to connect to the database. Retrying (9)... +3587msError: EPERM: operation not permitted, scandir '/proc/1/map_files/559e63542000-559e656d1000' at Object.readdirSync (fs.js:807:3) at GlobSync._readdir (/app/node_modules/glob/sync.js:288:41) at GlobSync._readdirInGlobStar (/app/node_modules/glob/sync.js:267:20) at GlobSync._readdir (/app/node_modules/glob/sync.js:276:17) at GlobSync._processReaddir (/app/node_modules/glob/sync.js:137:22) at GlobSync._process (/app/node_modules/glob/sync.js:132:10) at GlobSync._processGlobStar (/app/node_modules/glob/sync.js:380:10) at GlobSync._process (/app/node_modules/glob/sync.js:130:10) at GlobSync._processGlobStar (/app/node_modules/glob/sync.js:383:10) at GlobSync._process (/app/node_modules/glob/sync.js:130:10)

The same issue happens when I use mysql:latest & mysql:5.6. This is my connection:

TypeOrmModule.forRoot({
  type: 'mariadb',
  host: '172.17.0.1',
  port: 3306,
  username: 'nest',
  password: 'secret',
  database: 'nest',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true,
}),

Has anyone else ever had this issue, and so, know how to fix it? Thanks!


Solution

  • This problem occurs because of entities: [__dirname + '/**/*.entity{.ts,.js}'],. Because __dirname does somehow not resolve to your projects directory but '' instead, it will try to scan your whole drive for entities and obviously does not have the permissions to do so.

    Workaround

    Instead of scanning for entities, list them explicitly:

    entities: [UserEntity, ProductEntity],
    

    If you are using webpack for hmr, you can also try the following config to resolve __dirname correctly:

    webpack.config.js:
    node: {
      __dirname: false,
    },