I understand that this might sound sarcastic or rude, but I promise I mean this genuinely! Is there anywhere you can train with him where there aren't loose dogs? This is a super normal thing for an adolescent dog, but it's going to be incredibly difficult to work through this issue if he's constantly being (unintentionally) rewarded by play and interaction with loose dogs. And, as you've experienced, the risk of another dog being actively aggressive towards him is very real, and possibly physically dangerous - next time it might not be a small dog. He's still at a very impressionable age and negative encounters with other dogs could impact his behavior around them for life, so if you can at all find a way to avoid areas with roaming dogs, that'll be a huge improvement for multiple reasons.