In the 15 months since Donald Trump shut him out of a White House job, Michael Cohen , the president’s personal lawyer, has made a show of publicly dining with a vocal Trump critic, the billionaire Dallas Mavericks owner Mark Cuban .

After lunch at Freds restaurant in New York in April 2017, a gossip item appeared in the New York Post. When they had breakfast at the Time Warner Center in November, paparazzi “somehow” showed up, Mr. Cuban said. “I think he does it to piss off Trump when Trump is ignoring him.”

The second meeting, reported by celebrity-news outlet TMZ, did catch Mr. Trump’s attention. He called Mr. Cohen to complain. The attorney sought to reassure him, according to a person familiar with the conversation. “No, boss, I had breakfast with him to set him straight. I told him he has to respect the office, to respect you,” Mr. Cohen said, according to this person.

“Boss, I miss you so much,” he said later in the conversation. “I wish I was down there with you. It’s really hard for me to be here.”

Federal prosecutors are investigating Mr. Cohen’s work for Mr. Trump, including his $130,000 pre-election payment to a former adult-film actress, as well as his taxi business and other personal dealings. Federal Bureau of Investigation agents raided his premises earlier this month.

Mark Cuban and Michael Cohen are photographed leaving New York’s Landmarc restaurant in November. Photo: BACKGRID

Looming over all of this is an investigation by special counsel Robert Mueller into whether associates of Mr. Trump, including Mr. Cohen, colluded with Russians to influence the 2016 presidential election. Mr. Cohen and the White House have denied any collusion.

Prosecutors may not find evidence of wrongdoing by either man. Mr. Trump and his lawyers nonetheless are now grappling with the question of how the president’s self-described “fixer” may respond if charged and presented with the choice of turning on his boss or facing prison time.

Mr. Cohen has memorably said he would “take a bullet” for the president. But in a sign of Mr. Cohen’s state of mind, he has in recent months privately groused about being excluded from White House posts he believed he deserved, according to people familiar with his thinking. He has struggled to get Mr. Trump’s attention. And two new business engagements he started during that time that could have profited from his Washington connections have instead languished.

The Wall Street Journal reported last week that a longtime Trump legal adviser had warned the president that Mr. Cohen would likely cooperate if charged. Since then, Mr. Trump called Mr. Cohen “a fine person with a wonderful family” on Twitter and expressed confidence he would stand strong.

“Most people will flip if the Government lets them out of trouble, even if…it means lying or making up stories,” Mr. Trump tweeted. “Sorry, I don’t see Michael doing that despite the horrible Witch Hunt and the dishonest media!”

Michael Cohen, left, exits federal court in New York on April 16. Photo: Victor J. Blue/Bloomberg News

Mr. Cohen declined to be interviewed and provided a two-word response to a list of questions sent by email: “Completely inaccurate.” He didn’t respond to a request to elaborate.

The White House didn’t respond to a request for comment.

The recent actions have represented a remarkable turnabout for Mr. Cohen since the November 2016 election of Mr. Trump, whose presidential aspirations he had been encouraging for years.

Mr. Cohen had no official role in Mr. Trump’s 2016 campaign but informally advised him and served as a surrogate on TV. Behind the scenes, he tried to kill damaging stories about Mr. Trump, including reaching out to discourage women who had spoken negatively about the candidate, according to several of the women. He also paid for the silence of Stephanie Clifford , the former porn star known as Stormy Daniels, who alleges she had a 2006 sexual encounter with Mr. Trump.

Mr. Cohen and the White House have denied any sexual encounter took place.

Steve Bannon and Reince Priebus both got jobs in Donald Trump’s White House. Photo: BRENDAN SMIALOWSKI/Agence France-Presse/Getty Images

In the months before the election, when Mr. Trump reshuffled his campaign for a third time and named Steve Bannon as campaign chief, Mr. Cohen told associates he had expected to be tapped for the role, according to people familiar with the matter. He also told people at the time he expected to be named White House chief of staff, people familiar with the matter said.

Mr. Trump decided that bringing Mr. Cohen inside the White House carried too many risks, according to people familiar with the discussions. Mr. Trump privately has described Mr. Cohen as a “bull in a china shop,” who when brought in to fix a problem sometimes breaks more china, according to a person close to the president.

On Jan. 5, 2017, two weeks before the inauguration, Mr. Cohen, camped in his cluttered office on the 26th floor of Trump Tower, a few doors away from the president-elect, still didn’t know what his future role would be.

Mr. Cohen juggled two phones with the backdrop of mixed martial-arts paraphernalia in his office. Mr. Trump “doesn’t operate with timelines,” Mr. Cohen explained to a reporter. Then he corrected himself: “I am not a timeline item,” he said. “He knows that an hour before he leaves, if he calls me and says, ‘I need you in D.C.,’ I’ll be there.”

About a week later, Mr. Cohen had grown more frustrated. He still hadn’t solidified his role. “I still don’t know exactly what I’m going to do, whether I need to stay here for a while or go to D.C.,” he said in a phone interview. “It’s crazy we’re talking about this three, four days before which everybody starts heading down and I have no idea.”

Michael Cohen on an elevator at Trump Tower, where the presidential transition was playing out in December. Photo: Drew Angerer/Getty Images

Mr. Cohen said he believed he might become Mr. Trump’s personal attorney, which he said would keep their communications confidential. “My sole purpose is to protect him and the family from anyone and anything,” he said in the January interview.

The conversation occurred less than three months after his deal with Ms. Clifford—then not publicly known—in which Mr. Cohen has said he used his home-equity line to finance an agreement in which she promised not to discuss her allegation of a 2006 sexual encounter.

Federal investigators are now probing whether the payment violated campaign-finance or other laws. Mr. Cohen has denied wrongdoing.

In a Fox News interview Thursday, Mr. Trump said of the Cohen investigation: “I’ve been told I’m not involved.” He added of his longtime lawyer: “From what I can tell, he did nothing wrong.”

Since the recent raids, which were approved by a federal judge, Mr. Cohen and Mr. Trump have argued that the government punctured attorney-client privilege in its seizure of his records.

Mr. Cohen has long surrounded himself with the trappings of wealth. In the Long Island enclave where he grew up, prep-school classmates recall him being part of a crowd of kids who flaunted family money and wore flashy brands. Mr. Cohen drove to law school in a Jaguar, two of his law school classmates recall.

Mr. Cohen, who as a teenager frequented Brooklyn’s ethnic Russian neighborhoods and married into a Ukrainian family, cultivated a rough-and-tumble, streetwise image.

Gregory Ehrlich invited Mr. Cohen to his wedding and was amused to hear he bragged to another guest that he belonged to the Russian mob. Mr. Ehrlich, who is now estranged from Mr. Cohen, said he doesn’t believe his former friend had any such ties.

Mr. Cohen joined the Trump Organization in 2007, after years of practicing law and doing business deals on his own, holding the title of Mr. Trump’s special counsel.

Mr. Trump didn’t always demonstrate respect for his employee. After saying he’d attend Mr. Cohen’s son’s bar mitzvah in 2012, Mr. Trump was late, and the blessings were delayed, according to an attendee.

Michael Cohen after a meeting with the Senate Intelligence Committee to discuss Russian electoral interference was postponed. Photo: Tom Williams/CQ Roll Call/Newscom/ZUMA PRESS

After Mr. Trump arrived, he gave a speech, telling guests he hadn’t planned to come, but he relented after Mr. Cohen had repeatedly called him, his secretary and his children begging him to appear, the attendee said. The guests laughed because “everyone knew it was very realistic-sounding,” the attendee added.

Mr. Cohen found himself on the outside during the presidential transition, said a person close to Mr. Trump. He tried to find a role for himself by building bridges between Mr. Trump and the business community, but never got traction, this person said.

During the inaugural festivities, Mr. Cohen and his guests weren’t given priority access, the person said, noting that the hurt was visible on Mr. Cohen’s face: “He was always just at the edges.”

Now on his own, Mr. Cohen has had mixed success in business since Mr. Trump went to Washington. He re-established a solo legal practice, taking space on the 23rd floor of a skyscraper in Rockefeller Center, in the law and lobbying firm Squire Patton Boggs, less than half a mile from his old office at Trump Tower.

Under a “strategic alliance” with Squire Patton Boggs, Mr. Cohen received an annual fee of $500,000 from the firm for business development, according to court documents and a firm spokesman. His agreement with the firm also entitled him to a percentage of the fees the firm received from the five clients he referred, according to court documents.

Michael Cohen chats with friends outside his Manhattan hotel. Photo: Yana Paskova/Getty Images

His work for the firm appeared to be anchored in Washington, D.C. He was recruited by Edward Newberry, a top lobbyist in D.C. and global managing partner at the firm, said a person familiar with the matter. A firm spokesman declined to make Mr. Newberry available for comment.

Some firm partners opposed the alliance, because they weren’t privy to the terms of the agreement and feared Mr. Cohen’s reputation would jeopardize that of Squire Patton Boggs, people familiar with the matter said.

Asked for comment, a firm spokesman referred the Journal to a recent court filing by prosecutors opposing Mr. Cohen’s challenge to the search warrants executed against his property. The filing says Mr. Cohen “maintained complete independence” from the firm, used a separate computer server and had an office that locked with a key the law firm didn’t have.

Earlier this month, on the day of the FBI raid, Squire Patton Boggs said it was ending its relationship with Mr. Cohen, “mutually and in accordance with the terms of the agreement.”

Mr. Cohen also went into business with 4C Health Solutions, a Midlothian, Va.-based startup that works with governments and companies to help them detect questionable or fraudulent billing.

A consulting agreement reviewed by the Journal entitles Mr. Cohen to a 5% commission on contracts he delivers for the company, as well as the possibility of earning equity. The company told the Journal last year that it was in the hunt for large federal contracts, but more recently it has focused on states and the private sector, said Chief Executive David Adams.

Mr. Adams said Mr. Cohen has advised 4C on capital structure and introduced executives to potential clients, but the company has “paid zero dollars to Mr. Cohen,” because he hasn’t brought in any business.

The company last year said Mr. Cohen would join the board but later reversed itself to avoid unwanted attention, according to Tommy Thompson, the former Republican Wisconsin governor, who is chairman of 4C’s board.

Mr. Cohen, asked about the consulting agreement in January, said in an email he was “confused as to how or why you question my involvement in a startup company.”

It is unclear what work, if any, Mr. Cohen performed for Mr. Trump since the election. There is no known record of his involvement in legal matters for the president since the start of the Trump administration.

In the Jan. 5, 2017, interview, Mr. Cohen said: “Let’s just say I have no shortage of work. It encompasses all aspects of his life from his business to the personal,” adding: “It’s private between Mr. Trump and myself unless it’s made public because of a lawsuit or a news story.”

Mr. Trump said Thursday on Fox News that Mr. Cohen has done “a tiny, tiny little fraction” of his legal work, including “this crazy Stormy Daniels deal he represented me on.”

Mr. Cohen was one of Mr. Trump’s fiercest defenders last year, on Twitter and elsewhere. On May 24, he urged followers to retweet “if every morning you wake up and thank the Lord that @POTUS @realDonaldTrump won the election! I do…”

Related Video On Monday President Trump called the raids at the office of his lawyer, Michael Cohen, a "disgrace" and a "witch hunt" and discussed the possibility of firing special counsel Robert Mueller. Photo: Getty.

Privately, Mr. Cohen already had begun complaining to associates, both about being left in New York and about Mr. Trump’s then-failure to repay him for the $130,000 he had drawn off his home-equity line to pay Ms. Clifford, people familiar with the matter say.

Mr. Cohen even was contemplating “defecting” from Mr. Trump, according to a person familiar with these conversations. Mr. Cohen stopped complaining about Mr. Trump not repaying him around mid-2017, according to another person familiar with the situation. Mr. Cohen has said he wasn’t repaid by the Trump Organization or Mr. Trump’s campaign, but has declined to answer questions about whether Mr. Trump himself repaid him.

In the weeks since the FBI raid, Mr. Cohen has been out on the town, smoking cigars with friends and frequenting tony restaurants such as Nobu in midtown, in what some who know him interpret as an attempt to show he isn’t frightened of what the investigation will bring.

—Rebecca Ballhaus and Lisa Schwartz contributed to this article.

Write to Michael Rothfeld at michael.rothfeld@wsj.com, Alexandra Berzon at alexandra.berzon@wsj.com and Joe Palazzolo at joe.palazzolo@wsj.com