MongoDB error: No suitable servers found (`serverSelectionTryOnce` set): [socket timeout calling hello on ‘localhost:27017’]. Topology type: Single

So I have a dockerized mongodb.

My docker-composer.yml code is:

  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: unless-stopped
    environment:
      MONGO_INITDB_ROOT_USERNAME: xxxx
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb/data:/data/db
    logging:
      driver: "json-file"       # Use the JSON file logging driver
      options:
        max-size: "200k"        # Rotate log files when they reach 200KB
        max-file: "10"          # Keep up to 10 rotated log files

When I connect to it from with this command, it give me the list of databases:
docker exec -it mongodb mongosh “mongodb://wmd_mongo_admin21:T8d_jm5_y0Clz3De2x@localhost:27017/Test?authSource=admin” –eval “db.getCollectionNames()”

But when I try to connect to it using php driver:

$mongodb_url = "mongodb://xxxx:xxxx@localhost:27017/Test";

try {
    // Create MongoDB manager
    $manager = new MongoDBDriverManager($mongodb_url);

    // Test query on 'urls' collection
    $query = new MongoDBDriverQuery([]);
    $cursor = $manager->executeQuery('Test.urls', $query);
    
    echo "Mongodb successfully connected!n";

    foreach ($cursor as $doc) {
        print_r($doc);
    }
} catch (MongoDBDriverExceptionException $e) {
    echo "Failed to connect or query MongoDB. Error: " . $e->getMessage() . "n";
}

It give me this error:

Failed to connect or query MongoDB. Error: No suitable servers found (`serverSelectionTryOnce` set): [socket timeout calling hello on '127.0.0.1:27017']. Topology type: Single

The PHP driver works because I can create a MongoDB manager

$manager = new MongoDBDriverManager($mongodb_url);

The problem is when I try to execute a query

$query = new MongoDBDriverQuery([]);
$cursor = $manager->executeQuery('Test.urls', $query);

Please note that the php script was working perfectly and stop working suddently without me changing the code.