I don't entirely agree with this point (though I think you'd have a stronger case for armies), however I agree that this is not Starfleet's primary purpose and therefore I think that it is unlikely that Starfleet maintains personnel trained only for this role (ie infantry) but rather multi-disciplinary personnel that can take on that role should the situation demand it.
And one of the most efficient ways of combating those realities, is to ensure that people who find themselves in those situations have the best training, equipment and skillsets to allow them to survive and to protect others.
'(Good) people who when they need to will defend everyone' existing outside of the accountability of a military (or law enforcement) chain of command are vigilanties. While I like a good comic book as much as the next person, there are too many cases of this type of thing being ruled by individual prejudices and tragic misunderstandings for me to believe that it's a 'better' model.
As you say, 'or law enforcement' in my personal experience, the police are not a military, as that is not how they work where I am from. I also agree with the coastguard comparisons above, and while Hornblower and the age of sail are without a doubt part of treks inspiration, I find that more to be about the stories than the organisational structures we see on screen.
Though I do believe the historical figure who served as part of Hornblowers inspiration, once pulled something of a Kirkesque stunt.
I don't mind military fictions, understand the need for a military in real world terms. I also have an opinion on why it is not needed in trek.
Your skill set training argument is fine for what we think of as 'good' soldiers. And I couldn't agree more.
However, if a uniform, a rank structure, a weapon, an organisation of such define a military (as is argued often as defining starfleet) then there are corners of this earth that have those things, and are decidedly not on the side of good.
In star trek, all humanity are basically the good guys, because they act as such, not because they wear the uniform.
It's complex, but in treks case, starfleet being a military brings nothing to the table, and it specifically not being a military, actually does in terms of the ideals that the storytelling tries to follow.