Doeg was an official who worked for King Saul. Saul commanded him to kill the innocent priests at Nob (1 Samuel 21–22). Doeg was an Edomite. which means he was either a foreigner who converted to the Israelite religion or a prominent leader from Edom who was captured during Saul's military campaign against the Edomites (14:47).
Saul put Doeg in charge of his flocks of animals (21:7). King David also later had a foreigner in charge of his cattle (1 Chronicles 27:30).
The Bible does not clearly explain why Doeg was at the sanctuary at Nob (1 Samuel 21:7). He might have been there for a religious purpose. If he was going through a purification process, this may have forced him to stay here (like a Nazirite vow described in Numbers 6:13). Another possibility is that he was hiding there as a spy for King Saul.
What we do know is that while at Nob, Doeg saw the priests welcome David and give him food and a weapon—the sword of Goliath (1 Samuel 21:9). A short time later, Doeg had opportunity to report this to Saul (22:9–10; Psalm 52 title). He hoped to demonstrate his loyalty with this report.
Doeg's character is revealed when he brutally killed the priests and all the people living in the city of Nob (1 Samuel 22:18–19). This cruel action suggests he was not truly an Israelite, even if he lived among them.