Do military titles get capitalized?

This is a style issue, and the appropriate style for you to use depends on which style guide you or your publisher or school generally follows and whether that guide specifies a rule for handling military (or other hierarchical) titles. The influential (in the United States) Chicago Manual of Style, fifteenth edition (2003), lays out one approach:

Titles and Offices

8.21 Capitalization: the general rule/ Civil, military, religious and professional titles are capitalized when they immediately precede a personal name and are thus used as part of the name (usually replacing the title holder's first name. Titles are normally lowercased when following a name or used in place of a name ...

[Examples:] President Lincoln; the president; General Bradley; the general; Cardinal Newman; the cardinal Dean Mueller; the dean; Governors Edgar and Ryan; the governors

Exceptions exist, according to Chicago, but these primarily involve instances of direct address (Yes sir, Captain!") or formal introduction ("Ladies and Gentlemen, the Prime Minister"), as well as various special forms of displayed lists or promotional presentations.

In a normal situation in which you were referring to a particular commander as "the commander" (or "the Commander"), Chicago advises using "the commander."

The Oxford Guide to Style (2003) agrees with Chicago on capitalization of titles used immediately before a name and noncapitalization of titles identified immediately after a name:

4.1.7 Titles of rank or relationship, and nicknames:

...

Titles used as identification or clarification after a name normally are not capitalized (especially in US usage):

[Examples:] Miss Dunn, the head teacher; Anne Williams, our mnaging director; Mr. Gladstone, the prime minister; Dr Primrose, the parish vicar

Titles used before a name are normally capitalized, and are not followed by a comma:

[Examples:] Head Teacher Miss Dunn; Managing Director Anne Williams; the Prime Minister Mr. Gladstone; The Reverend Dr Primrose

But Oxford breaks with Chicago on the very point that the OP asks about:

Capitals are preferred, however, when a short-form mention of a title is used as synonym for a particular person, an organization in an institutional or official sense, or a government:

[Relevant examples:] the Duke; the Princess, the Ministry (of Defence); the Centre's policy

So if you already introduced Commander Shepard earlier in a piece of writing, and you now wish to refer to the same person as "the Commander," Oxford advises you to capitalize the C in Commander.

In the United States, I believe, the Chicago style is more prevalent than the Oxford style. It is certainly the style we followed at the tech magazines where I've worked. But in the UK, the Oxford style may be dominant.