Casey James Fury, 24, of Portsmouth, N.H., faces two counts of arson at the Portsmouth Naval Shipyard, charges that carry a sentence of up to life in prison.

The Naval Criminal Investigative Service says Fury confessed to setting a fire inside the USS Miami while it was in dry dock May 23, as well as setting the second blaze outside the sub on June 16.

At a U.S. District Court hearing in Portland on Wednesday, his lawyer asked that Fury be released on home confinement while he awaits trial. The attorney, David Beneman, likened Fury to a middle school student who pulls a fire alarm hoping to get out of a test.

But Magistrate Judge John Rich III ordered Fury held without bail, saying he was especially concerned that the defendant went on to set the second fire after the first caused so much damage.

Fury, a painter and sand blaster, told NCIS investigators that he was suffering from anxiety and set the first fire to avoid completing his shift stripping paint in the submarine’s forward torpedo room, according to prosecutors.

The fire quickly got out of control and the steel hull trapped heat, causing superheated smoke and a stubborn fire that took more than 100 firefighters to douse.

It remains to be seen if the Los Angeles-class attack sub will be repaired or scrapped.

An affidavit in federal court indicates Fury walked with investigators through another Los Angeles-class sub, the USS Pasadena, where he demonstrated where he’d set fire to rags on a bunk bed before returning to his post in the torpedo room.

By the time the fire alarm sounded, the smoke in a passageway was so thick that Fury and a co-worker had to find another route to safety, departing through an “escape trunk” farther back on the submarine, the affidavit said.

The submarine was undergoing a 20-month overhaul at the Navy shipyard in Kittery, Maine. The fire was confined to forward compartments and did not reach the back of the submarine where the nuclear propulsion components are located.

Fury said he set the second fire outside the submarine at the dry dock cradle after a text-message exchange with an ex-girlfriend about a man she had started seeing, according to the affidavit. That fire caused little damage.