Donkeys typically bite when they feel threatened or are in danger.
It's a defensive reaction rather than unprovoked aggression. Like many animals, a donkey is more likely to bite if it's frightened or feels it needs to protect itself, so approaching calmly and respectfully helps avoid that response.