0

Basically, mongo-auth via rootUser/created user works fine locally, but fails on my server after deployment via Coolify/Dokploy Here is my docker compose file:

version: '3.8'
services:
  mongodb-dev:
    image: mongo:latest
    container_name: mongodb-dev
    restart: always
    # env_file: ${ENV_FILE:-.env}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
    ports:
      - '27017:27017'
    volumes:
      - mongodbdata:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro //This should be executed by Mongo during startup
    networks:
      - my-network

  my-nest-backend:
    build: .
    container_name: my-nest-backend
    ports:
      - '${PORT}:${PORT}'
    depends_on:
      - mongodb-dev
    networks:
      - my-network
    # env_file: ${ENV_FILE:-.env}
    environment:
      DATABASE_NAME: ${DATABASE_NAME}
      DATABASE_USER: ${DATABASE_USER}
      DATABASE_PASS: ${DATABASE_PASS}
      MONGODB_HOST: ${MONGODB_HOST}
      NODE_ENV: ${NODE_ENV}
      PORT: ${PORT}
      MONGO_URI: mongodb://${DATABASE_USER}:${DATABASE_PASS}@${MONGODB_HOST}:27017/${DATABASE_NAME}?authSource=admin&directConnection=true
    command: npm run start:debug

volumes:
  mongodbdata:

networks:
  my-network:
    driver: bridge

NestJS:

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRootAsync({
      useFactory: async () => {
        const logger = new Logger('MongooseModule');
        const uri = process.env.MONGO_URI;

        try {
          // Establish a MongoDB connection with the provided URI
          return {
            uri,
            useNewUrlParser: true,
            useUnifiedTopology: true
          };
        } catch (error) {
          logger.error(`MyLog Failed to connect to MongoDB: ${error.message}`, error.stack);
          throw error; // Rethrow to prevent app from starting with bad DB connection
        }
      },
    }),
    QuoteModule,
    PhilosopherModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements OnModuleInit {
  private readonly logger = new Logger(AppModule.name);

  async onModuleInit() {
    this.logger.log('MyLog AppModule initialized');
  }
}

Fail without any details:

MongoServerError: Authentication failed.
        at Connection.sendCommand (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:297:27)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at async Connection.command (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:325:26)
        at async executeScram (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/auth/scram.js:79:22)
        at async ScramSHA1.auth (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/auth/scram.js:39:16)
        at async performInitialHandshake (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:101:13)
        at async connect (/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:19:9)

What can be wrong?

1 Answer 1

0

because your local db is not the same as the remote db, try to use an external service for the db like mongo atlas. so in the deployed version maybe you dosen't hve the user

Sign up to request clarification or add additional context in comments.

2 Comments

Wdm not the same? By the way, everything works without authorization, or if I run my nest.js app in dev build instead of the prod
i need more info to debug that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.