NestJS .env not connecting to Docker Container DB

Viewed 661

im working on a nestjs project currently and im trying to connect it to a mysql/mariadb database with typoeorm right now which is running in a docker container (aswell as the project itself).

following the documentation of nestjs "configuration" i installed the package via the cli and imported it into the app.module.ts

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { User } from './auth/user.entity';
import { MoviesModule } from './movies/movies.module';

@Module({
  imports: [
    ConfigModule.forRoot({ //<- imported the config module
      isGlobal: true,
    }),
    TypeOrmModule.forRoot({
      type: 'mariadb',
      host: process.env.DB_HOST, //<- now trying to use the .env files data 
      port: parseInt(process.env.DB_PORT),
      username: process.env.DB_USER,
      password: process.env.DB_PASS,
      database: process.env.DB_NAME,
      entities: [User],
      synchronize: true,
    }),
    AuthModule,
    MoviesModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

my .env looks like this

DB_USER=root
DB_PASS=example
DB_HOST=mariadb
DB_NAME=lizenzdb
DB_PORT=3306

i spun up the docker containers via docker-compose, my docker-compose looks like this:

version: '3.5'

services:
  etk-be:
    build: ./backend/
    image: etk-lizenzdb-backend:final
    container_name: etk-be
    restart: unless-stopped
    ports:
      - 3000:3000
    networks: 
      - etk

  mariadb:
    image: mariadb
    container_name: mariadb
    command: --default-authentication-plugin=mysql_native_password
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: lizenzdb
    ports:
      - 3306:3306
    volumes:
      - ./data/db:/var/lib/mysql
    networks:
      - etk

  adminer:
    image: adminer
    container_name: adminer
    restart: unless-stopped
    ports:
      - 8080:8080
    networks:
      - etk

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    restart: unless-stopped
    ports:
      - 8081:80
    environment:
      - PMA_HOST=mariadb
      - PMA_USER=root
      - PMA_PASSWORD=example
    networks:
      - etk

networks:
    etk:
        driver: bridge

i tried connecting the project and database outside the docker containers already and it worked. all i have to change for the docker container is the db host from localhost to the name of the docker container itself (mariadb), but it wont connect to the database

1 Answers

You have the .env file, however, you need to let docker know to use it. I am adding an example of it to show how you can use it.


services:
  etk-be:
    build: ./backend/
    image: etk-lizenzdb-backend:final
    container_name: etk-be
    restart: unless-stopped
    ports:
      - 3000:3000
    networks: 
      - etk
    env_file: 
      - /path/to/envfile

  mariadb:
    ..

env file documentation

Related