There is the story of a certain pious man who groaned (suffered) from a pain in his heart, and when the doctors were consulted they said that there was no remedy for him unless he suckled warm milk from a goat every morning. They brought a goat and tied it to the leg of his bed and he used to suckle milk from it. The next day his friends came to visit him. When they saw the goat they said: ‘There is an armed bandit in the house and we are entering to visit him?’ They left him immediately. When he died they sat down and made investigation and found no other sin in him except that of the keeping of the goat. The pious man, too, at his death said: ‘I myself know that I have not sinned except in the keeping of this goat, having transgressed the teaching of my colleagues,’ for the Sages taught: One may not raise small domesticated animals in the Land of Israel.
There are differences of opinion as to why the Sages forbade raising sheep or goats in Israel. Rashi explains this based on the commandment to settle the land of Israel. Since these animals are known as being difficult to contain at home and they may escape and destroy crops, it is forbidden to raise them, even if they are confined. According to the Rambam, it is simply a matter of these animals causing damage, but the Sages limited their prohibition only to Israel where the vast majority of inhabitants are Jewish. According to both opinions, the prohibition is limited to areas that are settled, but in the desert or in the wild it would be permitted.
There is some discussion about whether it is permissible to raise sheep and goats in Israel today. While in Har Tzvi Rav Tzvi Pesach Frank permits it, arguing that the Rabbinic prohibition has expired and no longer applies, in his Yabia Omer Rav Ovadiah Yosef rules that it is still in effect.