Donald Trump's presidency of the United States has lurched from crisis to crisis since he took office less than two years ago, but the resignation of James Mattis was a landmark day of chaos that appeared to test the resolve of even senior Republican backers in Washington.

The Defense Secretary, a widely respected figure seen as a stabilising influence inside the administration, handed in his resignation after arguing with Trump over foreign policy in a White House meeting.

Mattis then released a letter that showed fundamental policy differences between the two men and implicitly criticised Trump's disregard for allies abroad.

The soft-spoken, but battle-hardened former Marine, always stood in uneasy contrast with his brash businessman boss.

And after months of speculation and following Trump's stunning decision to pull out of Syria and slash troops in Afghanistan, the gulf between the two men finally grew too wide to bridge.

Mattis, a 68-year-old native of Washington state, was one of Trump's first cabinet picks and has spent nearly two years at the Pentagon, where he became a master of concealing from the public his true thoughts on Trump's decisions.

But his resignation letter spelled out to the world what seemed obvious to many observers: Trump's world view was irreconcilable with his own.

"My views on treating allies with respect and also being clear-eyed about both malign actors and strategic competitors are strongly held and informed by over four decades' immersion in these issues," Mattis wrote to Trump, who has sought closer ties with Russia and heaped contempt on NATO and other alliances.

"Because you have the right to have a Secretary of Defense whose views are better aligned with yours on these and other subjects, I believe it is right for me to step down from my position."

Mattis does not directly reference Trump in his resignation letter, other than to point out that he shares a similar view that the US should not be the "policeman of the world".

But, he writes, "we must use all tools of American power to provide for the common defense, including providing effective leadership to our alliances".

He also warns that the US must be "resolute and unambiguous" in its relations with countries "whose strategic interests are increasingly in tension with ours".

It is clear that China and Russia, for example, want to shape a world consistent with their authoritarian model . . . to promote their own interests at the expense of their neighbors, America and our allies".

He adds a none too subtle hint at Trump's inexperience, pursuit of isolationism and tendency to be easily influenced when he writes:

We must do everything possible to advance an international order that is most conducive to our security, prosperity and values, and we are strengthened in this effort by the solidarity of our alliances

When Mattis interviewed with Trump for the job in 2016, he split with the president-elect on a host of issues, including on NATO and the use of torture.

Trump ultimately deferred to Mattis, who opposed the practice, signaling that he could be persuaded by his advisers.

But as time grew, Trump increasingly acted on his own instincts on a host of national security issues, choosing an "America First" agenda that contradicted Mattis' core beliefs.

Mattis was nonetheless seen as a voice of moderation capable of reining in some of Trump's more rash impulses.

His appointment was greeted with a collective sigh of relief in Washington, following a brutal presidential campaign in which Trump vowed to upend the US-led world order.

As a highly decorated veteran, Mattis won the immediate respect of Trump, who has a fascination with all things military, dating back to his youth when his father sent him to a private military academy in New York.

Major Gen Mattis in discussions with the Iraqi army in Falluja in 2004

Mattis enlisted in the Marines in 1969 during the height of the Vietnam War, whereas Trump got five deferments from being drafted.

And until the end, Mattis avoided the contempt Trump had publicly heaped on other of his top officials.

Trump thanked Mattis for his service, saying he would be "retiring, with distinction" - a far cry from when he reportedly fired former secretary of state Rex Tillerson by tweet.

Alliances and diplomacy

Mattis believed in the established tenets of the American world order: alliances were sacred, diplomacy was better than combat, and military assertiveness required a cautious but firm hand, ready to act decisively when all else fails.

He clashed with Trump repeatedly, mostly behind the scenes, over those fundamentals.

While Trump spoke positively of working with Russian President Vladimir Putin, Mattis warned that Russia wants to "break NATO apart" and, uncommonly, made his objections public when Trump failed to endorse the US commitment to a common defense in the Atlantic treaty organisation.

In his resignation letter, he wrote:

"NATO's 29 democracies demonstrated that strength in their commitment to fighting alongside us following the 9-11 attack on America, The Defeat-ISIS coalition of 74 nations is further proof."

Mattis also pushed back when Trump appeared ready to leave a dangerous security gap in pursuit of a nuclear deal with North Korea.

The chasm only grew as he opposed Trump's decision to withdraw from the Paris climate change accord, decertify the Iran deal, slap tariffs on steel and aluminum, and move the US Embassy in Israel from Tel Aviv to Jerusalem.

He also quietly rejected the president's proposed ban on transgender service members, standing by a small, poorly represented part of the 2.1 million active and reserve members of the US military.

'Mad Dog' and 'warrior monk '

Trump originally latched on to Mattis for his commitment to building up the US military after years of tight budgets, and for his nickname: "Mad Dog."

A colourful commander, he earned the moniker - which he told some he didn't really like - with his battle-hardened swagger and the sort of blunt language Marines are famous for.

"Be polite, be professional, but have a plan to kill everyone you meet," he famously said.

But the lifelong bachelor was also known as a "warrior monk."

Gen Mattis meets Jordanian Army Lt Mohammed Al-Zhoun (L) in Afghanistan in 2002

A scholar of warfare, he is said to have a personal library of more than 7,000 volumes, and issued required reading lists to Marines under his command, instructing them that the most important territory on a battlefield is the space "between your ears."

Mattis commanded a Marine battalion during the First Gulf War and a Marine division during the 2003 invasion of Iraq.

In 2010, he was named to head the US Central Command. That gave him authority over troops in Iraq, where he helped develop a counter-insurgency approach before overseeing the US withdrawal, and Afghanistan, where he implemented a troop surge.

An expert communicator adept at tailoring his message for his audience, whether it be a soldier in the field or a foreign dignitary, Mattis would gently cajole Trump into following orthodox US military doctrine.

Mattis last year helped persuade Trump to stay the course in Afghanistan, though Trump ultimately followed his initial instinct to try to get out.

Against Mattis's counsel, Trump ordered troops to the US border with Mexico ahead of the November election in what was widely viewed as a political stunt to show he was tough on immigration.

Then came Trump's decision this week to declare victory over the Islamic State group in Syria and pull US troops out - which Mattis sees will leave a vacuum that could be filled by America's enemies.

Senate Majority Leader Mitch McConnell issued a statement expressing grave reservations about why Mattis left.

"I am particularly distressed that he is resigning due to sharp differences with the president on ... key aspects of America’s global leadership".

It was a vivid reminder of the early months of Trump's White House, when he fired his first national security adviser, Michael Flynn, after only days in office, fired then-FBI Director James Comey and launched a travel ban against majority Muslim countries that was blocked by the courts.

Trump already faces a difficult 2019, likely to be dominated by a special prosecutor's investigation into whether his 2016 campaign colluded with Russia and congressional probes into his businesses, his family and some cabinet members.

Since the midterm elections, Trump has fired Attorney General Jeff Sessions and White House Chief of Staff John Kelly after they lost his confidence, as well as Interior Secretary Ryan Zinke, who is under an ethics cloud.

Others who were previously seen as on the way out may stay longer.

Trump is said by aides to be pleased by Homeland Security Secretary Kirstjen Nielsen for toughening her approach to the border, and Commerce Secretary Wilbur Ross is unlikely to be forced out unless he wants to leave.

But Mattis' resignation was jarring.

White House spokeswoman Sarah Sanders told reporters that she did not believe Trump had asked Mattis to resign.

"The president had a good relationship (with Mattis), but sometimes they disagree. The president always listens to the members of his national security team, but at the end of the day it is the president's decision to make," she said.