MADRID — The scene is becoming all too familiar, and now it has gripped Spain: Streets are empty, shops are shuttered, families are cloistered at home and the numbers of those infected with the coronavirus, and those who have died, are growing.

Spain — like Italy and France to varying degrees — is now on lockdown, struggling to contain a virus that already seems out of control, with about 8,000 people infected and almost 300 dead. In the center of Madrid, police patrolled the streets, with few cars passing along its main artery, the Castellana. In Barcelona, the Gothic Quarter was empty, and private security kept anyone from entering the cathedral.

Spain’s prime minister, Pedro Sánchez, said he was imposing drastic measures to fight the coronavirus, forcing people to stay indoors, with exemptions only for so-called exceptional circumstances.

“The situation is no joke,” said Sergio González, 26, as he walked his dog in Barcelona, a mask covering his face.