-1

I'm trying to call abha-services from parchaa-ndhm but I'm encountering the error. I've even made a network named my_network and all the containers are in my_network but the call from parchaa-ndhm is not reaching to abha-services. what can be wrong in this docker compose file or is there anything to do with nestjs

parchaa-ndhm   | [Nest] 55  - 03/09/2023, 1:58:05 PM   ERROR [ExceptionsHandler] connect ECONNREFUSED 127.0.0.1:8081
parchaa-ndhm   | Error: connect ECONNREFUSED 127.0.0.1:8081
parchaa-ndhm   |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)

below is my docker compose file

version: "3.7"
services:
    abha-services:
        platform: linux/amd64
        build: 
            context: .
            dockerfile: ./apps/abha-services/Dockerfile
        container_name: abha-services
        hostname: abha-services
        command: npm run start abha-services
        env_file:
            - .env
        ports:
            - '8081:8081'
        networks:
            - my_network
    parchaa-ndhm:
        platform: linux/amd64
        build: 
            context: .
            dockerfile: ./apps/parchaa-ndhm/Dockerfile
        container_name: parchaa-ndhm
        hostname: parchaa-ndhm
        command: npm run start parchaa-ndhm
        env_file:
            - .env
        ports:
            - '1506:1506'
        links:
            - abha-services
            - user-services
        networks:
            - my_network
    user-services:
        platform: linux/amd64
        build:
            context: .
            dockerfile: ./apps/user-services/Dockerfile
        container_name: user-services
        hostname: user-services
        command: npm run start user-services
        env_file:
            - .env
        depends_on:
            - postgres
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        ports:
            - '8082:8082'
        networks:
            - my_network
    postgres:
        image: postgres:13-alpine
        restart: always
        container_name: postgres
        hostname: postgres
        platform: linux/amd64
        volumes:
            - ./data/postgres:/var/lib/postgresql/data
        environment:
            - POSTGRES_USER=xxxxxx
            - POSTGRES_PASSWORD =xxxxxxxx
            - POSTGRES_DB =xxxxxx
        networks:
            - my_network
networks:
  my_network:
    driver: bridge

this is the client code of parchaa-ndhm to abha-services

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'ABHA-SERVICES',
        transport: Transport.TCP,
        options: {
          host:'abha-services',
          port: 8081
        }
      }
    ])
  ],
  controllers: [RegistrationMobileController],
  providers: [RegistrationMobileService]
})
export class RegistrationMobileModule {}


also here is my bootstrap class

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AbhaServicesModule,
    {
      transport: Transport.TCP,
      options: {
        host:'0.0.0.0',
        port: 8081,
        retryAttempts: 3,
        retryDelay: 1000
      }
    },
  )
  await app.listen()
  console.log('abha-services started')
}
bootstrap();

I had try mentioning host in clientModule and bootstrap function of abha-services

  • You haven't shown the client code at all, but you seem to be telling the client to connect to itself (127.0.0.1) and not the other container. Also see [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation. – David Maze Mar 09 '23 at 14:29

1 Answers1

0

From what i see is that your parchaa-ndhm tries to reach abha-services via 127.0.0.1:8081 but since they all run in a docker environment i guess you need to tell parchaa-ndhm that the service is reachable under abha-services:8081.

Viper
  • 2,216
  • 1
  • 21
  • 41
  • I'm telling parchaa-ndhm that the host is abha-services and port 8081 but it is giving me the same error @Module({ imports: [ ClientsModule.register([ { name: 'ABHA-SERVICES', transport: Transport.TCP, options: { host:'abha-services', port: 8081 } } ]) ], controllers: [RegistrationMobileController], providers: [RegistrationMobileService] }) export class RegistrationMobileModule {} – Rajveer -parchaa Mar 09 '23 at 14:22