President Donald Trump began Thursday under siege, listening to howls of indignation from conservatives over his border wall and thrusting the government towards a shutdown. He ended it by announcing the exit of the man US allies see as the last guardrail against the president's erratic behaviour: Defence Secretary Jim Mattis, whose resignation letter was a scathing rebuke of Mr Trump's world-view.

At perhaps the most fragile moment of his presidency - and vulnerable to convulsions on the political right – Mr Trump single-handedly propelled the US government into crisis and sent markets tumbling with his gambits this week to salvage signature campaign promises.

The president's decisions and conduct have led to a fracturing of Mr Trump's coalition. Hawks condemned his sudden decision to withdraw US troops from Syria. Conservatives called him a "gutless president" and questioned whether he would ever build a wall. Political friends began privately questioning whether Mr Trump needed to be reined in.

After campaigning on shrinking America's footprint in overseas wars, Mr Trump abruptly declared on Wednesday that he was withdrawing US troops from Syria, a move Mr Mattis and other advisers counselled against. And officials said on Thursday that Mr Trump is preparing to send thousands of troops home from Afghanistan as well.

The president also issued an ultimatum to Congress to fund construction of his promised US-Mexico border wall, a move poised to result in a government shutdown just before Christmas. Mr Trump and his aides had signalled tacit support for a short-term spending compromise that would avert the shutdown, but the president abruptly changed course after absorbing a deluge of criticism from some of his most high-profile loyalists.

Leon Panetta, who served as former defence secretary, CIA director and White House chief of staff for Democratic presidents, said, "We're in a constant state of chaos right now in this country." He added, "While it may satisfy [Trump's] need for attention, it's raising hell with the country."

Mr Panetta said the resignation of Mr Mattis is a singular moment and his letter, which underscores how Mr Mattis sees Mr Trump's approach as misguided, "puts the security of the nation right now at some degree of risk."

Mr Trump has been isolated in bunker mode in recent weeks as political and personal crises mount, according to interviews with 27 current and former White House officials, Republican lawmakers and outside advisers to the president, some of whom spoke on the condition of anonymity to offer candid assessments.

"There's going to be an intervention," one former senior administration official said speculatively. "Jim Mattis just sent a shot across the bow. He's the most credible member of the administration by five grades of magnitude. He's the steady, safe set of hands. And this letter is brutal. He quit because of the madness."

In his resignation letter, Mr Mattis wrote, "My views on treating allies with respect and also being clear-eyed about malign actors and strategic competitors are strongly held and informed by over four decades of immersion in these issues . . . Because you have the right to a Secretary of Defence 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."

Who could be running against Trump in 2020? Show all 23 1 /23 Who could be running against Trump in 2020? Who could be running against Trump in 2020? Joe Biden The former vice president - poised to be a frontrunner - has announced his run. He recently faced scrutiny for inappropriate touching of women, but was thought to deal with the criticism well AFP/Getty Who could be running against Trump in 2020? Bernie Sanders The 2016 runner-up has announced that he will be running again in 2020 Getty Who could be running against Trump in 2020? Hillary Clinton The 2016 Democratic presidential candidate and former Secretary of State says she is “still considering” whether she will run again. Getty Who could be running against Trump in 2020? Pete Buttigieg The Indiana mayor and war veteran will be running for president. If elected, he would be the first openly LGBT+ president in American history. Getty Who could be running against Trump in 2020? Kamala Harris The former California attorney general will be running for president in 2020. Introduced to the national stage during Jeff Sessions’ testimony, she has endorsed Medicare-for-all and proposed a major tax-credit for the middle class. AP Who could be running against Trump in 2020? Elizabeth Warren The Massachusetts Senator has formally launched her bid for president in 2020. A progressive Democrat, she is a major supporter of regulating Wall Street. AP Who could be running against Trump in 2020? Beto O’Rourke The former Texas congressman told Oprah Winfrey that he “has been thinking about running for presidency”, but stopped short of formally announcing his bid to run in 2020. AFP/Getty Who could be running against Trump in 2020? Wayne Messam Mayor of the city of Miramar in the Miami metropolitan area, Wayne Messam has announced his bid. He intends to run on a progressive platform against the "broken" federal government. He favours gun regulations and was a signatory to a letter from some 400 mayors condemning President Trump's withdrawal from the Paris Climate Accord. Vice News Who could be running against Trump in 2020? Kirsten Gillibrand The New York Senator formally announced her presidential bid in January, saying that “healthcare should be a right, not a privilege.” Getty Who could be running against Trump in 2020? Cory Booker The New Jersey Senator has announced that he will be running for the presidency in 2020. If he secures the nomination he said finding a female vice president would be a priority. Getty Who could be running against Trump in 2020? John Delaney The Maryland congressman was the first to launch his bid for presidency, making the announcement in 2017. AP Who could be running against Trump in 2020? Julian Castro The former San Antonio mayor announced his candidacy in January and said that his running has a “special meaning” for the Latino community in the US. Getty Who could be running against Trump in 2020? Tulsi Gabbard The Hawaii congresswoman announced her candidacy in January, but is likely to face tough questions on her past comments on LGBT+ rights and her stance on Syrian President Bashar al-Assad. Getty Who could be running against Trump in 2020? Andrew Yang The entrepreneur has announced his presidential candidacy, and has pledged that he would introduce a universal basic income of $1,000 a month to every American over the age of 18. AFP/Getty Who could be running against Trump in 2020? Marianne Williamson The author and spiritual advisor has announced her intention to run for president. She had previously run for congress as an independent in 2014 but was unsuccessful. Getty Who could be running against Trump in 2020? John Kerry The former secretary of state has said he is still thinking about whether to run. Getty Who could be running against Trump in 2020? Michael Bloomberg The entrepreneur and former New York mayor– with a net worth of around $50bn – has said he will decide by the end of February whether to seek the presidency. AFP Who could be running against Trump in 2020? Howard Schultz Former Starbucks CEO Howard Schultz has not yet ruled out running for president in 2020, despite criticism that his bid could help re-elect Mr Trump by dividing the Democrat vote. AP Who could be running against Trump in 2020? Eric Holder The former attorney general has said he will decide in “the next month or so” whether to run as a 2020 presidential candidate. AP Who could be running against Trump in 2020? Eric Swalwell The California congressman said he is “ready to do this” and will decide before April whether to run. MSNBC Who could be running against Trump in 2020? Terry McAuliffe The former Virginia governor, who worked to elect Democratic governors during 2018 midterms, said there was a “50 per cent” chance he would run. AP Who could be running against Trump in 2020? Sherrod Brown The Ohio senator is still undecided about whether to run for president in 2020. Who could be running against Trump in 2020? Mitch Landrieu The former New Orleans mayor said he doesn’t think he will run for president, but “never say never”. AP Photo/Jacquelyn Martin

Although Mr Trump's relationship with Mr Mattis has been rocky for months, the president spent the first part of Thursday focused on another fraying relationship: with his conservative base.

On Thursday, as criticism over his capitulation on the wall grew louder by the hour, Mr Trump complained to friends and aides that he felt politically shackled. He had no plan, but was spoiling for a fight. By midday, the president picked one.

"I've made my position very clear: Any measure that funds the government must include border security. Has to," Mr Trump said on Thursday. He added he had "no choice" but to act.

Mr Trump's advisers acknowledged the funding may not be secured in the end, but boasted that the spectacle would be remembered favourably by his base voters as proof of his mettle. A wall funding proposal was approved by the House late on Thursday, but faced uncertain prospects in the Senate.

Senate Majority Leader Mitch McConnell, R-Ky, habitually careful in his approach to Mr Trump, avoided strong-arming the president in their recent exchanges, knowing that urging him to stand down on the wall funding likely would only embolden him, according to two people familiar with the discussions. At every turn, Mr McConnell confided to Mr Trump that congressional efforts this month - from the passage of the farm bill to bipartisan criminal-justice reform - were a string of victories for him, the people said.

Thanks to Mr McConnell's soothing, there was cautious optimism that the president would eventually sign a funding bill. "McConnell has a lot to do with it, of course. They talk a lot," Representative Harold Rogers, R-Ky, said. "It's smart to save the fight for another day."

House Speaker Paul Ryan, R-Wis, - busy this week bidding farewell to Congress after two decades in office - followed Mr McConnell's playbook, pointing out how Mr Trump would be able to battle for wall funding in the new year, a person close to him said.

Both Mr Ryan and House Majority Leader Kevin McCarthy, R-Calif, told Mr Trump last week they could not pass a spending bill with $5 billion for wall construction, and Mr McConnell was told by the White House that Mr Trump would sign a short-term bill without it, according to two people familiar with the conversations.

But on Fox News Channel and across conservative media, there was a brewing rebellion. Prominent voices urged Mr Trump to hold firm on his wall money and warned caving would jeopardise his re-election.

Rush Limbaugh dismissed the compromise bill on his radio program as "Trump gets nothing and the Democrats get everything." Another firebrand, Ann Coulter, published a column titled, "Gutless President in Wall-less Country." Mr Trump even found resistance on the couch of his favourite show, Fox & Friends, where reliable Trump-boosting host Brian Kilmeade chided him on the air on Thursday.

The president was paying attention. He promptly unfollowed Ms Coulter on Twitter. And he pecked out a series of defensive tweets blaming congressional leaders for not funding the wall, while also staking a defensive posture. He suggested a massive wall may not be necessary in its entirety because the border already is "tight" thanks to the work of Border Patrol agents and troops.

"One of the things he's most vulnerable to is mockery and mockery by his own supporters," said Mark Krikorian, a leading anti-immigration activist.

'They're all coming back and they're coming back now' Trump declares victory over Isis in Syria

Members of the conservative House Freedom Caucus joined in the rebellion. During House votes on Wednesday evening, caucus chair Representative Mark Meadows, R-NC, and other members refused to budge and held court with reporters, railing against the Republican leadership and warning Mr Trump he was being led astray.

At a Thursday Oval Office meeting with Mr Ryan and Mr McCarthy, Mr Trump was calm and did not yell, but was resolute and "dug in on what he wants," said a Republican official briefed on the discussion.

Mr Trump spent six to seven minutes in the meeting with Mr Ryan and Mr McCarthy talking about "steel slats" and saying the term was preferable to calling the proposed construction a "wall," as the president has done for more than three years.

The administration drama comes at an especially perilous juncture for Mr Trump, following his thumping at the polls in November's midterm elections. Democrats are preparing to take control of the House in January and to use their subpoena power to investigate Mr Trump's finances and conduct in office, as well as alleged corruption in the administration.

Meanwhile, Republicans in their last gasp of unified government are divided. A band of hard-line conservative lawmakers voted against Mr Trump's prison reform legislation, which passed the House and Senate with bipartisan support and is expected to be signed by the president on Friday. And many foreign policy hawks, including Senator Lindsey Graham, R-SC, a Trump confidant, sharply criticised Mr Trump's decision on Wednesday to withdraw US troops from Syria.

Inside the Oval Office on Thursday, Mr Trump was in what one Republican close to the White House described as "a tailspin," acting "totally irrationally" and "flipping out" over criticisms in the media.

Even as aides argued to him that protesting over wall funding could deprive government workers pay-checks over Christmas, Mr Trump warned in private conversations with Republican lawmakers they all would get "crushed" if they did not get the wall built.

Mr Trump vented to his advisers that signing the short-term spending bill without wall money would make him look weak and make his base voters think he broke a campaign promise. He also complained he did not have sufficient bargaining leverage with Congress and blamed senior aides - including White House legislative affairs director Shahira Knight - for not presenting him with better options.

"He's legitimately saying, 'This is ridiculous. Why would I sign this?'" said American Conservative Union Chairman Matt Schlapp, whose wife, Mercedes Schlapp, is a senior White House staffer. "If you're the outsider, you don't want to go along with the same old stupid rules. When he does that, he doesn't look like an outsider."

Marc Short, Mr Trump's former White House legislative affairs director, said, "He feels he made a promise to voters that he would build the wall. There is a realisation that with Congress changing hands, that the challenge of getting funding for the wall only gets larger."

Mr Trump's pin-balling conduct - veering from a promise of a shutdown to moving towards a deal, and then changing his mind - has vexed members of his own party. Many Republican lawmakers said they sense Mr Trump is adjusting uneasily to the dynamics of soon-to-be divided government and lashing out to reclaim some of his dominance over the congressional agenda.

The defence secretary's resignation compounded the concern.

"Having Mattis there gave all of us a great deal more comfort than we have now," retiring Senator Jeff Flake, R-Ariz, said. "Chaos has kind of been the norm but it seems to have been heightened. Sometimes you think it's got to settle, and then another thing happens."