Goblins and such like have been popularised in more recent culture such as Lord of the Rings (Orcs and Uruk-hai), but where did the semblance originate from globally, as there is speak of goblins in medieval european texts and also other cultures around the same time?
There are a number of European words related to or reminiscent of the modern English term "goblin." In general, European folklore conceives of these as underground mining spirits akin to their above ground counterparts who appear with various terms including elf, fairy, pixy - to name only a few.
In folk tradition, these entities - whether above or below ground - were regarded as extremely dangerous, but they were not regarded as evil. If approached with respectful deference (or better if not approached at all!), the entities could be beneficial as they frequently warned of the collapse of underground excavations and sometimes they led miners to valuable ore. All of these supernatural beings were fickle, however: their attitudes could turn quickly and they punished even the most minor slight - perceived or real. Here is an illustration from 1873 of a Cornish knocker, one of the more famous types of "goblin" in Britain. The above ground counterparts of these sorts of mining spirits were particularly inclined to abduct people and were equally feared.
Because mines are dangerous, eerie places, it was easy for modern popular culture to perceive these underground supernatural miners as slipping into the realm of evil. This process dates back at least to the publication of George MacDonald's The Princess and the Goblin, originally published in 1872; Here is an image of the Goblins in his book: they were evil unlike their folklore counterparts. This process continued with Tolkien's books, of course: his fantasy world, "Middle Earth," tended to separate supernatural forces into good and evil realms in a way that the folk generally did not. Because of McDonald and likely because of the dangerous, deeply feared underground entities, it was easy for Tolkien to carry this forward, characterizing his goblins (and related names/entities) as the ultimate in evil and menacing. With a bit of irony, the above ground elves (who were equally feared by the folk) became Tolkien's beautiful, very positive entities. This was made easy by the nineteenth-century process that transformed elves, fairies, and pixies into cute, delightful entities that were far removed from their menacing counterparts in folk tradition.
I'm not prepared to discuss the literary side of this beyond what I have written here. But in answer to the core of your question: folk traditions about the supernatural in mines are probably as old as mining. German varieties are clearly documented Georgius Agricola in the sixteenth century. Before that, documentation is sparse. I published this article on Cornish and New World knockers/tommyknockers over a quarter century ago. This article was transformed, reflecting subsequent research and thinking and appearing in the final two chapters of my recent book, The Folklore of Cornwall: The Oral Tradition of a Celtic Nation (2018) - but unlike the book, the article is free!
I hope that helps.