“I credit Milley with convincing the president to modify his Syria decision,” said Jack Keane, the former Army vice chief of staff, who spoke several times with Mr. Trump and General Milley last month during the frenzied days of the president’s zigzagging Syria policy.

Nearly three years into the Trump presidency, the Pentagon is learning how to manage a capricious president whose orders can whipsaw by the hour. Top Defense Department officials have acquired their education the hard way, through Mr. Trump’s Twitter bullying of Iran and North Korea, letdown of allies in Syria, harsh attacks on the Atlantic alliance and public support for commandos the military has charged with war crimes. Mr. Trump, top Pentagon officials say, is unpredictable, frustrating and overly focused on spectacles like military parades.

But there is much these officials like about the president.

They are happy with the annual budget boost he gave them — to $716 billion this year from $585 billion in 2016 — and are pleased he has done away with what they considered micromanaging by Obama White House officials. Mr. Trump has also given commanders in combat zones a far freer hand to conduct raids. And among a big portion of the rank and file, those service members who mirror Mr. Trump’s conservative base, he remains very popular.

In many ways, the American military remains the part of the government most responsive to the president across a large and fractious administration, because civilian control of the armed forces is embedded in the Constitution and the psyche of every soldier. But for Mr. Trump, the other side of that coin is that the military respects the coequal branches of government, as Lt. Col. Alexander S. Vindman demonstrated in recent days when, against the wishes of the president, he testified in the House impeachment proceeding.