Dwarves could get some of their nutritional needs by growing fungi in dark caves.
And they could probably also grow more familiar crops underground.
Remember when Gimli sings the song of Durin in Moria?
There light of sun and star and moon,
in shining lamps of crystal hewn,
undimmed by cloud or shade of night,
there shone forever, fair and bright.
Gimli is saying that the Dwarves of Moria had artificial lighting in their underground halls. Thus it is possible that they grew food in underground farms using artificial grow lights, possibly powered by atomic energy.
And when Sauron invaded Eregon the Gates of Moria were shut. Certainly the west gates, and perhaps the east gates as well, if Saruon besieged Moria on the Dimrill Dale side also. The Dwarves would not be able to go outdoors to farm in the valley or trade for food. This should have lasted for decades or centuries until Sauron's threat ended.
The Dwarves of Moria should have grown their own food indoors under artificial lighting, and possibly even chemically synthized their food, using technology which was lost by the time of Gimli.
I expect that the orcs of the Misty Mountains may have sometimes had similar technology.
And of course the underground civilization of Angband which eventually supported armies of millions of orcs must have had a vast population supported by Morgoth's magic or advanced technology.