With Docker running a different URL

Asked

Viewed 23 times

0

I have an application running back end with Nestjs, Typeorm and Mongodb and my front end running React.

I created a Docker container to run all applications but I have a problem.

To run my local back end the connection url is mongodb://localhost:27017/db and for the Docker I need to use mongodb://Mongo:27017/db

Is there any way I can configure my Docker-Composer.yml to make this change?

Below my Docker Compose.

version: "3.7"
services:
  client:
    build:
      context: ./web
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3000:3000"
    working_dir: /web/src/app
    volumes:
      - ./web:/web/src/app
    entrypoint: ["npm", "start"]
    links:
      - api
    networks:
      - webappnetwork
  api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3333:3333"
    volumes:
      - ./backend:/backend
      - /backend/node_modules
    depends_on:
      - mongodb
    networks:
      - webappnetwork
  mongodb:
    image: mongo
    restart: always
    container_name: mongodb
    volumes:
      - ./data:/data/db
    ports:
      - 27018:27017
    command: mongod --noauth
    networks:
      - webappnetwork
networks:
  webappnetwork:
    driver: bridge

Below my bd connection file.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CardsModule } from './cards/cards.module';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    CardsModule,
    TypeOrmModule.forRoot({
      type: 'mongodb',
      url: 'mongodb://localhost:27017/db',
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: true,
      useUnifiedTopology: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Thank you all!

1 answer

0

Ideal is that you use environment variables to configure your application, such as database configuration for example.

A simple way would be to change DB settings to:

...
    TypeOrmModule.forRoot({
      type: 'mongodb',
      url: `mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/db`,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: true,
      useUnifiedTopology: true,
    }),
...

So in development you use environment variables with your dev configuration:

DB_HOST=localhost
DB_PORT= 27017

And when deploying on your Docker-Compose you pass new environment variables:

...
  api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3333:3333"
    volumes:
      - ./backend:/backend
      - /backend/node_modules
    depends_on:
      - mongodb
    networks:
      - webappnetwork
    environment:
      DB_HOST: mongodb
      DB_PORT: 27017
...

This is the right way to handle application configuration, now you just need to see how your framework helps you in this matter of setting up environment variables.

Making a quick google saw that Nestjs has a session in the documentation on the subject: https://docs.nestjs.com/techniques/configuration#Configuration

Browser other questions tagged

You are not signed in. Login or sign up in order to post.