WASHINGTON — Back when their relationship was fresh and new, and President Trump still called his defense secretary “Mad Dog” — a nickname Jim Mattis detests — the wiry retired Marine general often took a dinner break to eat burgers with his boss in the White House residence.

Mr. Mattis brought briefing folders with him, aides said, to help explain the military’s shared “ready to fight tonight” strategy with South Korea, and why the North Atlantic Treaty Organization has long been viewed as central to protecting the United States. Using his folksy manner, Mr. Mattis talked the president out of ordering torture against terrorism detainees and persuaded him to send thousands more American troops to Afghanistan — all without igniting the public Twitter castigations that have plagued other national security officials.

But the burger dinners have stopped. Interviews with more than a dozen White House, congressional and current and former Defense Department officials over the past six weeks paint a portrait of a president who has soured on his defense secretary, weary of unfavorable comparisons to Mr. Mattis as the adult in the room, and increasingly concerned that he is a Democrat at heart.

Nearly all of the officials, as well as confidants of Mr. Mattis, spoke on condition of anonymity to discuss the internal tensions — in some cases, out of fear of losing their jobs.