I am trying to setup some containers for my NestJS + TypeORM + MongoDB environment by using Docker Compose in a Windows 11 host, but I am getting an ECONNREFUSED error:
nestjs-docker-api-1 | [Nest] 1 - 07/23/2022, 8:45:32 AM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
nestjs-docker-api-1 | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27018
nestjs-docker-api-1 | at Timeout._onTimeout (/usr/src/app/node_modules/mongodb/lib/sdam/topology.js:293:38)
nestjs-docker-api-1 | at listOnTimeout (internal/timers.js:554:17)
nestjs-docker-api-1 | at processTimers (internal/timers.js:497:7)
I have created the following Dockerfile to configure the NestJS API container:
FROM node:12
# Create app directory
WORKDIR /usr/src/app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
# Install app dependencies
RUN npm install
# Bundle app source
COPY . .
EXPOSE 8080
# Creates a "dist" folder with the production build
RUN npm run build
# Start the server using the production build
CMD [ "node", "dist/main.js" ]
And then I reference this from Docker Compose with the following docker-compose.yml:
version: '3.4'
services:
web:
image: img_1
build:
context: .
dockerfile: Dockerfile
environment:
NODE_ENV: production
# PORT: 3333
ports:
- 3333:3000
# command: nx serve backend
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
db:
image: mongo:latest
environment:
- MONGODB_DATABASE="test"
ports:
- 27017:27017
Finally, I set the TypeORM configuration to match with the Docker Compose file:
imports: [TypeOrmModule.forRoot({
type: 'mongodb',
url : 'mongodb://db:27017/nest',
entities: [],
synchronize: true,
useUnifiedTopology: true
}),PersonModule,UserModule,
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
}),],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
I tried a lot but the code doesn't work.Any help?