PORTLAND, Maine — A shipyard worker who set fire to rags aboard a nuclear submarine because he wanted to go home was sentenced to a little more than 17 years in federal prison Friday for the blaze that transformed the vessel into a fiery furnace, injured seven people and caused about $450 million in damage.

Casey James Fury also was ordered to pay $400 million in restitution by a judge who weighed his lack of criminal record and the severity of the fire before imposing a 205-month prison sentence.

The 25-year-old Fury, formerly of Portsmouth, N.H., pleaded guilty to setting the May 23 fire while the USS Miami was undergoing a 20-month dry dock overhaul at Portsmouth Naval Shipyard in Kittery.

The civilian painter and sand blaster told authorities that he wanted to go home because he was suffering from an anxiety attack. He told them he never envisioned such extensive damage when he used a lighter to set fire to a bag of rags that he left burning on a bunk in a state room.

The blaze quickly grew into an inferno spewing superheated smoke that billowed from hatches. It took 12 hours and the efforts of more than 100 firefighters to save the submarine. Seven people were hurt, the Navy has said.

Eric Hardy, a shipyard firefighter who suffered back and shoulder injuries fighting the blaze, called it the worst fire he had ever seen.

“The best way I could describe it, sir, is fighting a fire in a wood stove and climbing down the chimney,” Hardy told the judge.

Fury, who had been working in the torpedo room, fled to the safety of the pier, prosecutors said, and watched as firefighters went down hatches and into the burning Los Angeles class-attack submarine, staying inside for only minutes at a time because of smoke and blistering heat.

Hardy said the smoke was so thick that he couldn’t even see a foot in front of him and his flashlight was virtually useless. Firefighters had 20-minute air packs, but it was so hard to climb into the sub and move around inside that they were limited to two to three minutes of actual firefighting.

About three weeks later, Fury set a second fire outside the crippled sub, again because of anxiety. That fire caused no damage. He pleaded guilty to two counts of arson in November.

Assistant U.S. Attorney Darcie McElwee said it was telling that Fury set a second fire after the extensive damage caused by the first one.

But the defense lawyer David Beneman contended Fury suffered from depression and anxiety and that he never intended to harm anyone. Beneman described a “spin cycle” caused by Fury’s failure to receive adequate treatment.

Fury spoke briefly Friday, apologizing to the people who were hurt and saying he meant no disrespect to the Navy.

“From the bottom of my heart, I’m truly sorry for what I have done,” he said.

U.S. District Judge George J. Singal weighed the extreme damage caused by the fire against Fury’s lack of criminal record, which consisted of one drunken driving conviction, in finding a sentence in the middle of the 235 months sought by prosecutors and 188 months sought by the defense.

“It is only by the grace of God that no one else was more seriously hurt or killed,” the judge said.

When he completes his prison sentence, Fury will have to serve five years of supervised release. The $400 million in restitution was mandated by federal statute, but prosecutors don’t expect to collect anywhere near that sum.

The May 23 fire damaged forward compartments including living quarters, a command and control center and the torpedo room. It did not reach the rear of the Groton, Conn.-based submarine, where the nuclear propulsion components are located.

The Navy determined it was cost-effective to repair the vessel with a goal of returning it to service in the middle of 2015. But its future is now uncertain. Repairs have been postponed under mandatory budget cuts known as sequestration.

Rear Adm. Richard Breckenridge, a submarine group commander, said the ship’s extensive damage had ripple effects around the Navy, delaying maintenance on other vessels and leading to longer deployments for thousands of sailors.