The 26-year-old will now link up with City on their pre-season tour of the United States after agreeing personal terms and passing his medical.
“There has been strong interest from other clubs, but it has always been my ambition to play for Pep Guardiola,” Danilo told his new club’s official website.
“As soon as I heard of his interest, I knew immediately I wanted to be a City player,” he added.
His departure from the European champions had been confirmed by Real boss Zinedine Zidane on Saturday on the eve of his team’s meeting with Manchester United in California.
According to the BBC he moves to Manchester on a £26.5 million (29.5 million euros) deal.
Guardiola had made full-backs his top transfer priority in the close season having released Pablo Zabaleta, Gael Clichy and Bacary Sagna.
Danilo links up with City’s new recruit Kyle Walker, the Tottenham defender who signed a five-year contract for a fee that could rise with add-ons to £54 million (61m euros) this month.
Danilo, who joined European champions Real in 2015 from Porto, started just 17 Spanish league games last season with Dani Carvajal often preferred in the right-back role.
The Spanish international started ahead of Danilo in the Champions League final in Cardiff as Real Madrid beat Juventus 4-1 to lift their 12th European Cup.
Real are due to face City in Los Angeles next week.
Download our app and read this and other great stories on the move. Available for Android and iOS.