When Dr. Dre sold his Beats headphones company to Apple for $3 billion last year, his stake in that transaction made him the richest man in hip-hop. The deal was likely struck in some ivory tower boardroom, but the vehicle that propelled Andre Young (as his mother named him) to that apex of success was gangsta rap — the genre he helped pioneer and sell like hot cakes to mainstream white America.

Dre’s brand of rugged, explicit rap music was born far away from the corporate world in the small, decaying L.A. hub city of Compton. In the fiery new biopic “Straight Outta Compton,” the neighborhoods where Dre and his friends reside look and feel more like a police state — a powder keg ready to spark what they call “reality rap.”

Dramatizing the formation of his influential group N.W.A., the film is a sprawling, pulsating, nonstop feat of filmmaking that follows the young men to the heights of hedonistic rap stardom.

But it starts very small. Specifically in a drug house in 1986, where Dre’s childhood acquaintance Eric “Eazy-E” Wright is collecting a debt. In an instant, a police tank outfitted with a steel battering ram smashes through the front door, sending Eazy off running — and yearning for a way out of the small-time drug trade.

He finds what he’s looking for in Dre, who is tired of spinning cheesy ’80s R&B at the neighborhood disco. Played with steely-eyed focus by Corey Hawkins, Dre wants to perform music that mirrors the brutal everyday experience of living in Compton. As the movie depicts with dizzying handheld camerawork, young black men like Dre are regularly stopped for “looking like gangbangers,” and then cuffed and slammed onto the hot hoods of police cruisers by nightstick-wielding cops.

Using Eazy’s small savings of drug money, the duo buy studio time and form N.W.A. with O’Shea Jackson (aka Ice Cube), MC Ren and DJ Yella. Clubgoers in Compton like what they hear. Soon the guys hook up with a slimy manager, played with reptilian verve by Paul Giamatti, and release their breakthrough debut, “Straight Outta Compton.”

The result: They fill arenas and Tom Brokaw is on TV lambasting their celebration of excess and defiance. The group even receives a letter from the FBI demanding they stop performing the hit song “[Expletive] tha Police.” They play it anyway at their next concert.

Movies like this live and die on the casting of their famous protagonists — and “Straight Outta Compton” is pitch perfect. Jason Mitchell captures the strange dynamism of Eazy-E — he’s menacing but also provides jester-like comic relief. In what could’ve been a disaster, Ice Cube’s son, O’Shea Jackson Jr., plays his father’s part exceptionally, right down to Dad’s trademark smirk.

In the small pool of big-budget hip-hop biopics, “Straight Outta Compton” is near the top. For comparison, the Eminem-starring “8 Mile” and the Biggie Smalls feature “Notorious” come to mind. “8 Mile” smartly focused on a short snapshot of Eminem’s beginnings — a textured portrait of the artist clawing his way through underground Detroit. “Notorious” embraced the Wikipedia approach, piling up a highlight reel of Biggie’s entire career but never stopping to get the viewer invested.

“Straight Outta Compton” finds a healthy middle ground. The second half of the movie re-enacts N.W.A.’s well-documented misadventures in the record industry, including the group’s bitter breakup and Eazy’s battle with AIDS. It’s here where the 2½-hour running time lags a bit as director F. Gary Gray packs inasmuch as he can. There are fun scenes where Dre collaborates with future stars such as Snoop Dogg and Tupac Shakur. The actors portraying these hip-hop titans do so with uncanny mimicry, but it feels forced and unnecessary.

The power and impact of “Compton” rest in its scenes of resistance. Much of this takes place in the film’s riveting first hour. The armored police vehicle in the opening scene looks like something out of Fallujah — or Ferguson, for that matter. In fact, the violent street imagery coursing through the movie eerily alludes to the recent unrest following the deaths of Michael Brown and Eric Garner at the hands of police officers.

Even when the members of N.W.A. become rich and famous, they can’t escape the images of police brutality that occupy the film’s first half. From his mansion in 1991, Eazy grimaces as he watches the Rodney King beating on TV, and later the not-guilty verdicts. Los Angeles soon erupts in flames — the group’s music foretelling this cataclysm. At a news conference, Ice Cube says: “You can’t treat people like that and expect them not to rise up.”

These days, the real-life Dr. Dre (and Ice Cube, too) are millionaire entrepreneurs busy with corporate investments, movie deals and other ventures. But once, they were just young men looking for an outlet to vent. When the focus is on those moments, “Straight Outta Compton” soars.