1

So I am trying to deploy my nestjs project to digitalocean and having problems with connecting database. First I created network and then run the following command in my server

docker run --name postgre -d -p 5432:5432 --network backend -e POSTGRES_PASSWORD=0808 -e POSTGRES_DB=webs postgres

Then in .env.production I change the host to the created container name as follows

POSTGRES_HOST=postgre

and keep the rest the same as in .env.deployment. My env:

POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_DB=webs
POSTGRES_PASSWORD=0808

Then I am trying to run my project by pulling the Dockerfile which I pushed to my to dockerhub as follows:

docker container run -p 3000:3000 --network backend --env .env.production --name mindgame musnn/mindgame

And it gives out the following error:

[Nest] 1  - 01/07/2023, 10:02:59 AM   ERROR [ExceptionHandler] connect ECONNREFUSED 127.0.0.1:5432
SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432

How can I solve this?

My app.module.ts:

import { AuthModule } from './auth/auth.module';
import { UsersModule } from './user/user.module';
import { BoardModule } from './board/board.module';
import { MessagesModule } from './messages/messages.module';
import { BoardCards } from './board/board-cards.model';
import { UserCards } from './user/user-card.model';
import { SequelizeModule } from '@nestjs/sequelize';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { User } from './user/user.model';
import { Board } from './board/board.model';
import { Card } from './board/card.model';
import {ConfigModule} from "@nestjs/config";


@Module({
  imports: [
    ConfigModule.forRoot({
           envFilePath: `.env.${process.env.NODE_ENV}`
        }),
    SequelizeModule.forRoot({
            dialect: 'postgres',
            protocol: 'postgres',
            host: process.env.POSTGRES_HOST,
            port: Number(process.env.POSTGRES_PORT),
            username: process.env.POSTGRES_USER,
            password: process.env.POSTGRES_PASSWORD,
            database: process.env.POSTGRES_DB,
            models: [User, Board, Card, UserCards, BoardCards],
            autoLoadModels: true
        }),
       MessagesModule,
       BoardModule,
       UsersModule,
       AuthModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

My Dockerfile (taken from the the internet documentation):

#using official 'node' image, with the alpine 3.15 branch as base image for development stage
FROM node:18.12.1-alpine3.15 As development

# create working directory inside the container
WORKDIR /usr/src/app

#copy package.json and package-lock.json files
COPY package*.json ./

#install dependencies
RUN npm install --only=development

#copy all files from current directory into container
COPY . .

# build the application
RUN npm run build

# expose on port 3000
EXPOSE 3000


#using official 'node' image, with the alpine 3.15 branch as base image for production stage
FROM node:18.12.1-alpine3.15 As production

#define the default value for NODE_ENV
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

# create working directory inside the container
WORKDIR /usr/src/app

#copy package.json and package-lock.json files
COPY package*.json ./

# install only dependencies defined in 'dependencies' in package.json
RUN npm install --only=production

#copy all files from current directory into container
COPY . .

# copy files
COPY --from=development /usr/src/app/dist ./dist
 
# command to start the server using the production build 
CMD ["node", "dist/main"]

0 Answers0