Nest.js TypeORM connect to local database (on docker container)

  docker, docker-compose, mysql, nestjs, typeorm

I have local MySQL database created in docker container from following docker-compose.yml

version: '3.6'
services:
  mysql:
    environment:
      - MYSQL_DATABASE=root
      - MYSQL_ROOT_PASSWORD=changeme
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=changeme
    command:
      - --table_definition_cache=100
      - --performance_schema=0
      - --default-authentication-plugin=mysql_native_password
      - --innodb_use_native_aio=0
    volumes:
      - ./init:/docker-entrypoint-initdb.d
    container_name: mysqldb
    image: mysql

And it’s working fine – I can enter docker container, log in as root user (or dbuser) and create databases, tables, execute queries, etc. I also have Nest.js application which uses @nestjs/typeorm module to connect to database using following ormconfig.json:

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "dbuser",
  "password": "changeme",
  "database": "test",
  "synchronize": true,
  "entiries": ["src/**/*/entity.ts"]
}

And here is the problem – when I start application I get following error:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'dbuser'@'172.22.0.1' (using password: YES)

It seems that all data in ormconfig.json are correct – why my application can’t connect to database?

Source: Docker Questions

LEAVE A COMMENT